Javascript add extra argument

后端 未结 11 1036
暖寄归人
暖寄归人 2021-02-06 22:02

Lets take a look at this code:

var mainFunction = function() {
  altFunction.apply(null, arguments);
}

The arguments that are passed to \"mainF

11条回答
  •  再見小時候
    2021-02-06 22:29

    Update 2016: You must convert the arguments to an array before adding the element. In addition to the slice method mentioned in many posts:

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

    You can also use the Array.from() method or the spread operator to convert arguments to a real Array:

    var args = Array.from(arguments);
    

    or

    var args = [...arguments];
    

    The above may not be optimized by your javascript engine, it has been suggested by the MDN the following may be optimized:

    var args = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

提交回复
热议问题