Reputation: 955
I'm using a single structure dynamically for many function as shown below:
Parent[funcName](data, function(err) {
// further operations
});
The variable "data" in the function have 0, 1, 2 or 3 as per the function requirement. It works fine when single argument is passed in "data" but if more than 1, it shows error - "more argument are expected".
So how can I pass multiple arguments by using single variable? Is there any work around?
Upvotes: 0
Views: 2099
Reputation: 4553
You can use the spread operator
Parent[funcName](...data, function(err) {
// further operations
});
where data
is the array of all the parameters you have passed.
Upvotes: 4
Reputation: 5629
Just make use of the arguments
object:
function myFunction() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
or this way:
function foo(...args) {
return args;
}
foo(1, 2, 3); // [1,2,3]
greetings
Upvotes: 1