showing results for - "javascript infinite parameters"
Kierra
20 Jul 2016
1Functions can access an array-like object called arguments that contains all the arguments that they received
2
3function print_my_arguments(/**/){
4    var args = arguments;
5    for(var i=0; i<args.length; i++){
6        console.log(args[i]);
7    }
8};
9
10Some important points to note:
11
12- arguments isn't an actual array. You can convert it to an array with the following line:
13	var args = Array.prototype.slice.call(arguments);
14
15Slice is also useful if you want your array to only contain the non-named arguments that were received:
16
17    function foo(first_arg, second_arg /**/){
18        var variadic_args = Array.prototype.slice.call(arguments, 2);
19    }
20
21Not every browser can handle an arbitrarily large number of function parameters.
22Last time I tested this, in Chrome and IE there was a stackoverflow after some 200.000 arguments. 
23If your function can receive an arbitrarily large number of arguments,
24consider packing all of those arguments in an regular array instead.
25
26    Those /**/ comments that appear in the arguments lists for my examples are not mandatory. They are just a coding a convention that I use to mark my variadic functions and differentiate them from regular functions.
27
28    // A quick glance would suggest that this function receives no
29    // parameters but actually it is a variadic function that gets
30    // its parameters via the `arguments` object.
31    function foo(){
32        console.log(arguments.length);
33    }