Merc
Merc

Reputation: 17057

The `arguments` object changes if parameters change

I just discovered that the arguments object actually changes if one of the parameters change.

For example:

function some(a, b, c ){
  console.log(arguments);


  args = [ a, b, c ];
  a = new Date();

  console.log(arguments);
  console.log(args);
}

some(1,2,3 );

You will see that while args stays the same (expected behaviour), arguments actually change.

Questions:

Upvotes: 2

Views: 145

Answers (1)

elclanrs
elclanrs

Reputation: 94101

This is specified in the ECMA standard sec-10.6:

For non-strict mode functions [...] the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function’s execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object’s properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.

Upvotes: 4

Related Questions