Javascript add extra argument

后端 未结 11 1059
暖寄归人
暖寄归人 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:13

    Use Array.prototype.push

    [].push.call(arguments, "new value");
    

    There's no need to shallow clone the arguments object because it and its .length are mutable.

    (function() {
        console.log(arguments[arguments.length - 1]); // foo
    
        [].push.call(arguments, "bar");
    
        console.log(arguments[arguments.length - 1]); // bar
    })("foo");
    

    From ECMAScript 5, 10.6 Arguments Object

    1. Call the [[DefineOwnProperty]] internal method on obj passing "length", the Property Descriptor {[[Value]]: len, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true}, and false as arguments.

    So you can see that .length is writeable, so it will update with Array methods.

提交回复
热议问题