+ 1
Is it possible to state a function with different number of parametrs in JavaScript?
Is there a way to somehow decide the number of parameters in a JavaScript function like we can do for MATLAB functions? Also, is it possible to skip a parameter and insert the value for next parameter?
2 ответов
+ 1
If you want to skip an argument, just use "undefined" at its position.
+ 1
I am not familiar with MATLAB.
if you want to skip a parameter and insert the next parameter value, you should place the skipped parameter at last position when you define a function.
best way to deal with this kind of situation is to use object as parameter and use the object property as parameter inside function value.
function log(param) {
// use any param property here or
// simply skip any param object property
console.log(param.a);
console.log(param.b);
}
var obj = {
b: "some value"
}
add(obj);
console.log(param.a) will log undefined since you didn't pass property a
you can also pass an empty object, function will be still executed without any error.