Javascript add extra argument

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.

Leave a Comment