Is it possible to send a variable number of arguments to a JavaScript function?

后端 未结 12 739
[愿得一人]
[愿得一人] 2020-11-22 10:51

Is it possible to send a variable number of arguments to a JavaScript function, from an array?

var arr = [\'a\',\'b\',\'c\']

var func = function()
{
    //         


        
相关标签:
12条回答
  • 2020-11-22 11:21

    Update: Since ES6, you can simply use the spread syntax when calling the function:

    func(...arr);
    

    Since ES6 also if you expect to treat your arguments as an array, you can also use the spread syntax in the parameter list, for example:

    function func(...args) {
      args.forEach(arg => console.log(arg))
    }
    
    const values = ['a', 'b', 'c']
    func(...values)
    func(1, 2, 3)
    

    And you can combine it with normal parameters, for example if you want to receive the first two arguments separately and the rest as an array:

    function func(first, second, ...theRest) {
      //...
    }
    

    And maybe is useful to you, that you can know how many arguments a function expects:

    var test = function (one, two, three) {}; 
    test.length == 3;
    

    But anyway you can pass an arbitrary number of arguments...

    The spread syntax is shorter and "sweeter" than apply and if you don't need to set the this value in the function call, this is the way to go.

    Here is an apply example, which was the former way to do it:

    var arr = ['a','b','c'];
    
    function func() {
      console.log(this); // 'test'
      console.log(arguments.length); // 3
    
      for(var i = 0; i < arguments.length; i++) {
        console.log(arguments[i]);
      }
    
    };
    
    func.apply('test', arr);
    

    Nowadays I only recommend using apply only if you need to pass an arbitrary number of arguments from an array and set the this value. apply takes is the this value as the first arguments, which will be used on the function invocation, if we use null in non-strict code, the this keyword will refer to the Global object (window) inside func, in strict mode, when explicitly using 'use strict' or in ES modules, null will be used.

    Also note that the arguments object is not really an Array, you can convert it by:

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

    And in ES6:

    const argsArray = [...arguments] // or Array.from(arguments)
    

    But you rarely use the arguments object directly nowadays thanks to the spread syntax.

    0 讨论(0)
  • 2020-11-22 11:22

    There are two methods as of ES2015.

    The arguments Object

    This object is built into functions, and it refers to, appropriately, the arguments of a function. It is not technically an array, though, so typical array operations won't work on it. The suggested method is to use Array.from or the spread operator to create an array from it.

    I've seen other answers mention using slice. Don't do that. It prevents optimizations (source: MDN).

    Array.from(arguments)
    [...arguments]
    

    However, I would argue that arguments is problematic because it hides what a function accepts as input. An arguments function typically is written like this:

    function mean(){
        let args = [...arguments];
        return args.reduce((a,b)=>a+b) / args.length;
    }
    

    Sometimes, the function header is written like the following to document the arguments in a C-like fashion:

    function mean(/* ... */){ ... }
    

    But that is rare.

    As for why it is problematic, take C, for example. C is backwards compatible with an ancient pre-ANSI dialect of the language known as K&R C. K&R C allows function prototypes to have an empty arguments list.

    int myFunction();
    /* This function accepts unspecified parameters */
    

    ANSI C provides a facility for varargs (...), and void to specify an empty arguments-list.

    int myFunction(void);
    /* This function accepts no parameters */
    

    Many people inadvertently declare functions with an unspecified arguments list (int myfunction();), when they expect the function to take zero arguments. This is technically a bug because the function will accept arguments. Any number of them.

    A proper varargs function in C takes the form:

    int myFunction(int nargs, ...);
    

    And JavaScript actually does have something similar to this.

    Spread Operator / Rest Parameters

    I've already shown you the spread operator, actually.

    ...name
    

    It's pretty versatile, and can also be used in a function's argument-list ("rest parameters") to specify varargs in a nicely documented fashion:

    function mean(...args){
        return args.reduce((a,b)=>a+b) / args.length;
    }
    

    Or as a lambda expression:

    ((...args)=>args.reduce((a,b)=>a+b) / args.length)(1,2,3,4,5); // 3
    

    I much prefer the spread operator. It is clean and self-documenting.

    0 讨论(0)
  • 2020-11-22 11:22
    (function(window) {
      var proxied = window.alert;
      window.alert = function() {
        return proxied.apply(window, arguments);
      };
    }(this));
    
    alert(13, 37);
    
    0 讨论(0)
  • 2020-11-22 11:23

    For those who were redirected here from Passing variable number of arguments from one function to another (which should not be marked as a duplicate of this question):

    If you're trying to pass a variable number of arguments from one function to another, since JavaScript 1.8.5 you can simply call apply() on the second function and pass in the arguments parameter:

    var caller = function()
    {
        callee.apply( null, arguments );
    }
    
    var callee = function()
    {
        alert( arguments.length );
    }
    
    caller( "Hello", "World!", 88 ); // Shows "3".
    

    Note: The first argument is the this parameter to use. Passing null will call the function from the global context, i.e. as a global function instead of the method of some object.

    According to this document, the ECMAScript 5 specification redefined the apply() method to take any "generic array-like object", instead of strictly an Array. Thus, you can directly pass the arguments list into the second function.

    Tested in Chrome 28.0, Safari 6.0.5, and IE 10. Try it out with this JSFiddle.

    0 讨论(0)
  • 2020-11-22 11:24

    The apply function takes two arguments; the object this will be binded to, and the arguments, represented with an array.

    some_func = function (a, b) { return b }
    some_func.apply(obj, ["arguments", "are", "here"])
    // "are"
    
    0 讨论(0)
  • 2020-11-22 11:26

    It's called the splat operator. You can do it in JavaScript using apply:

    var arr = ['a','b','c','d'];
    var func = function() {
        // debug 
        console.log(arguments.length);
        console.log(arguments);
    }
    func('a','b','c','d'); // prints 4 which is what I want, then 'a','b','c','d'
    func(arr); // prints 1, then 'Array'
    func.apply(null, arr); 
    
    0 讨论(0)
提交回复
热议问题