ecmascript 5 - Why are JavaScript Arguments objects mutated by assignment to parameter? -


what rationale behind behaviour?

function f(x) {   console.log(arguments[0]);   x = 42;   console.log(arguments[0]); }  f(1); // => 1 // => 42 

perhaps genuine mistake. section of ecmascript specification defines behaviour?

actually, in strict mode, not happen you can see here.

if read section 10.6 of ecma standard, in particular note 1, you'll see:

for non-strict mode functions array index (defined in 15.4) named data properties of arguments object numeric name values less number of formal parameters of corresponding function object share values corresponding argument bindings in function‘s execution context. means changing property changes corresponding value of argument binding , vice-versa. correspondence broken if such property deleted , redefined or if property changed accessor property. strict mode functions, values of arguments object‘s properties copy of arguments passed function , there no dynamic linkage between property values , formal parameter values.

in short, saying that, in non-strict mode, named function parameters operate aliases items in arguments object. thus, changing value of named parameter change value of equivalent arguments item , vice versa. not mistake. expected behaviour.

as editorial, it's not idea rely on behaviour can lead confusing code. also, such code, if executed in strict mode, not longer work.


Comments