Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript infinite parameters

Functions can access an array-like object called arguments that contains all the arguments that they received

function print_my_arguments(/**/){
    var args = arguments;
    for(var i=0; i<args.length; i++){
        console.log(args[i]);
    }
};

Some important points to note:

- arguments isn't an actual array. You can convert it to an array with the following line:
	var args = Array.prototype.slice.call(arguments);

Slice is also useful if you want your array to only contain the non-named arguments that were received:

    function foo(first_arg, second_arg /**/){
        var variadic_args = Array.prototype.slice.call(arguments, 2);
    }

Not every browser can handle an arbitrarily large number of function parameters.
Last time I tested this, in Chrome and IE there was a stackoverflow after some 200.000 arguments. 
If your function can receive an arbitrarily large number of arguments,
consider packing all of those arguments in an regular array instead.

    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.

    // A quick glance would suggest that this function receives no
    // parameters but actually it is a variadic function that gets
    // its parameters via the `arguments` object.
    function foo(){
        console.log(arguments.length);
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: read and update csv file in nodejs 
Javascript :: how to use hidden in div in angular 
Javascript :: js toggle value 
Javascript :: xmlhttprequest pass parameters post 
Javascript :: mongoose delete property 
Javascript :: two digit js' 
Javascript :: lodash convert object to array 
Javascript :: node.js f string 
Javascript :: how to use static files in express with ejs 
Javascript :: electron open new window 
Javascript :: Reading Time with jquery 
Javascript :: run a code after delay js 
Javascript :: convert moment info to dd mmm yyyy 
Javascript :: react js create element 
Javascript :: gulp synchronous tasks 
Javascript :: convert string into bigNumber in ethers.js 
Javascript :: videojs cdn 
Javascript :: how to hide javascript code 
Javascript :: get if user signed in firebase 
Javascript :: typeahead cdn 
Javascript :: mongoose findbyidandupdate return updated 
Javascript :: angular 404 on refresh 
Javascript :: .call javascript 
Javascript :: js pow 
Javascript :: Properly upgrade node using nvm 
Javascript :: get random percentage javascript 
Javascript :: como ler um arquivo json com javascript 
Javascript :: js check if array 
Javascript :: how to use async await inside useeffect 
Javascript :: prevent multiple form submissions javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =