Arguments and References

Consider this JavaScript function:

var f = function (a) { console.log(a+" "+arguments[0]); a = 3; console.log(a+" "+arguments[0]); } 

I would expect a and arguments[0] refer to the same value only up to the second function statement. Instead, they always refer to the same value: f(2) calls

 2 2 3 3 

and f({foo: 'bar'}) calls:

 [object Object] [object Object] 3 3 

Argument identifiers and arguments identifier are associated in a special way?

+8
javascript
source share
1 answer

Argument identifiers and argument identifiers are associated in a special way?

Yes (but only in lax mode).

From the specification ( ES6 , ES5 ):

For non-strict function modes, the integer indexed properties of the arguments object data, the numerical values ​​of which are less than the number of formal parameters of the corresponding functional object, first separate their values ​​with the corresponding argument bindings in the execution context functions. This means that changing the property changes the corresponding value of the argument binding and vice versa. This correspondence does not work if such a property is deleted and then redefined or the property is changed to the accessor property. For strict mode functions, the property values ​​of the argument objects are just a copy of the arguments passed to the function, and there is no dynamic relationship between the property values ​​and the formal parameter values.

+7
source share

All Articles