Reputation: 7017
I will pass to a fuction mutiple sets of argumentes like this:
Myfunction(var1,var2)(var3,var4)(var5);
But i only know how to get the first set of aguments like this:
function Myfunction() {
for (var i = 0; i < arguments.length; i++) {
alert(arguments[i]);
}
}
and it will return var1, var2
How can i get the other set of aguments (var3,var4)(var5) etc... ?
My reference: JavaScript variable number of arguments to function
Upvotes: 0
Views: 2742
Reputation: 55779
The number of arguments you can supply to a function in JavaScript is effectively unlimited (no limit is defined in the spec, but this answer indicates that Chrome has a limit of 65535).
The arity of a function is the number of parameters specified in its definition. In JavaScript the arity of a function can be safely ignored when performing an invocation. You may supply as may arguments are you like.
You can get hold of all the arguments in the arguments
array-like object implicitly associated with every function invocation, or if you are using ES2015, then you can use the spread operator (as I do below).
The second feature of your question is that you are spreading a single larger operation across multiple invocations. This is known as currying and is a somewhat more advanced technique. In JavaScript you can leverage closures to achieve it.
This example may help you:
function add(...args1) {
if(!args1.length) {
return sum(args1);
}
return (...args2) => {
if(!args2.length) {
return sum(args1.concat(args2));
}
return add.apply(null, args1.concat(args2));
};
}
function sum(arr) {
return arr.reduce((p,c) => p+c,0);
}
console.assert(add(1,2)(3)(4)() === 10, '1+2+3+4 should be 10');
Upvotes: 1
Reputation: 3382
what you want is probably as simple as that
function myFunction(arg1, arg2, arg3, arg4, arg5) {
// use all your arguments here
}
If you want to have a pair of argument you could just send arrays as parameter like: myfunction([0, 1], [34, 21], [2,7])
To archieve a function call like you did you would need to do something like (see below), but that is totally insane.
function myFunction(arg1, arg2) {
return function(arg3, arg4) {
return function(arg5) {
}
}
}
Upvotes: 1
Reputation: 219057
I don't know what you think this code is doing:
Myfunction(var1,var2)(var3,var4)(var5)
But this is invoking three functions. Not one. To illustrate where your arguments would be available:
function MyFunction() {
// var1 and var2 are available here, because they were passed to this function
return function() {
// var3 and var4 were passed to THIS function
return function() {
// var5 was passed to THIS function
}
}
}
You can't access var3
, var4
, or var5
in MyFunction
because they weren't passed to that function. They were passed to the function(s) being returned by that function.
If you want to pass all of these arguments to MyFunction
then do so:
MyFunction(var1, var2, var3, var4, var5);
Then they would all be available:
function MyFunction() {
// var1, var2, var3, var4, and var5 are all available here
}
Upvotes: 2