javascriptarguments-object

Unexpected "arguments" property on object


In the following, the second and third console outputs seem to contradict:

function test() {

    console.log(arguments); // -> ["my", "arguments"]

    console.dir(this); // -> test function with arguments property set to null

    console.log(this.arguments); // -> ["my", "arguments"]

}

test.call(test, 'my', 'arguments');

As per my comments, inspecting the arguments property on this shows null, whilst logging this.arguments explicitly shows ["my", "arguments"].

What exactly is this when you invoke a function in such a way? I didn't expect this.arguments to contain the invocation arguments!


Solution

  • MDN says

    arguments as a property of Function can no longer be used.

    Therefore I wouldn't attempt to use this.arguments at all, but use the local function variable arguments. It's quite apparent that there's some magic going on to create arguments as well.