Pass unknown number of arguments into javascript function

后端 未结 12 1269
情话喂你
情话喂你 2020-12-02 10:17

Is there a way to pass an unknown number of arguments like:

var print_names = function(names) {
    foreach(name in names) console.log(name); // something li         


        
相关标签:
12条回答
  • 2020-12-02 10:52

    Much better now for ES6

    function Example() {
        return {
            arguments: (...args) =>{
                args.map(a => console.log());
            }
        }
    }
    
    var exmpl = new Example();
    exmpl.arguments(1, 2, 3, 'a', 'b', 'c');
    

    I hope this helps

    0 讨论(0)
  • 2020-12-02 10:57

    Rest parameters in ES6

    const example = (...args) => {
      for (arg in args) {
        console.log(arg);
      }
    }
    

    Note: you can pass regular parameters in before the rest params

    const example = (arg1, ...args) => {
      console.log(arg1);
      for (arg in args) {
        console.log(arg);
      }
    }
    
    0 讨论(0)
  • 2020-12-02 10:58

    There is a hidden object passed to every function in JavaScript called arguments.

    You would just use arguments.length to get the amount of arguments passed to the function.

    To iterate through the arguments, you would use a loop:

    for(var i = arguments.length; i--) {
       var arg = arguments[i];
    }
    

    Note that arguments isn't a real array, so if you needed it as an array you would convert it like this:

    var args = Array.prototype.slice.call(arguments);
    
    0 讨论(0)
  • 2020-12-02 10:59

    I like to do this:

    This will not help if you don't know the number of arguments, but it helps if you don't want to remember the order of them.

    /**
     *  @param  params.one        A test parameter
     *  @param  params.two        Another one  
     **/
    function test(params) {
    
        var one = params.one;
        if(typeof(one) == 'undefined') {
            throw new Error('params.one is undefined');
        }
    
        var two = params.two;
        if(typeof(two) == 'undefined') {
            throw new Error('params.two is undefined');
        }
    }
    
    0 讨论(0)
  • 2020-12-02 11:03

    ES3 (or ES5 or oldschool Javascript)

    You can access the arguments passed to any javascript function via the magic arguments object, which behaves similarly to an array. Using arguments your function would look like

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

    It's important to note that arguments is not an array. MDC has some good documentation on it: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Functions#Using_the_arguments_object

    If you want to turn arguments into an array so that you can do things like .slice(), .push() etc, use something like this:

    var args = Array.prototype.slice.call(arguments);
    

    ES6 / Typescript

    There's a better way! The new rest parameters feature has your back:

    var print_names = function(...names) {
        for (let i=0; i<names.length; i++) console.log(names[i]);
    }
    
    0 讨论(0)
  • 2020-12-02 11:03

    You can use the spread/rest operator to collect your parameters into an array and then the length of the array will be the number of parameters you passed:

    function foo(...names) {
        console.log(names);
        return names;
    }
    
    console.log(foo(1, 2, 3, 4).length);
    

    Using BabelJS I converted the function to oldschool JS:

    "use strict";
    
    function foo() {
      for (var _len = arguments.length, names = new Array(_len), _key = 0; _key < _len; _key++) {
        names[_key] = arguments[_key];
      }
    
      console.log(names);
      return names;
    }
    
    0 讨论(0)
提交回复
热议问题