Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected "arguments" property on object

Tags:

javascript

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!

like image 465
c24w Avatar asked Mar 26 '13 14:03

c24w


People also ask

What is the arguments object in JavaScript?

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 .

Can we pass object as parameter in JavaScript?

We can pass an object to a JavaScript function, but the arguments must have the same names as the Object property names.

What are object properties in JavaScript?

Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object.


2 Answers

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.

like image 167
Explosion Pills Avatar answered Sep 30 '22 13:09

Explosion Pills


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

The this keyword indeed refers to the test function - that's what you called it with. You can assert that by logging this === test.

So what's that arguments property? A very deprecated one that is set to the actual arguments object during a function invocation (and removed thereafter, which seems to be the reason why console.dir didn't capture it correctly). Don't use it, and don't care about it :-)

function test() {
    console.assert(this === test);
    console.assert(this.arguments === arguments);
    console.log(Object.getOwnPropertyDescriptor(this, "arguments"));
}
test.call(test, 'my', 'arguments');

// result (in Opera):
Object {
    configurable: false,
    enumerable: false,
    value: Arguments {
        0: "my",
        1: "arguments",
        callee: Function {…},
        length: 2
    },
    writable: false
}
like image 43
Bergi Avatar answered Sep 30 '22 13:09

Bergi