The `arguments` object changes if parameters chang

2019-05-28 01:12发布

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:

  • Is this something that is well documented? If so, where?

  • Is there anything else I need to be careful about the arguments object?

1条回答
老娘就宠你
2楼-- · 2019-05-28 01:40

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.

查看更多
登录 后发表回答