Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are JavaScript Arguments objects mutated by assignment to parameter?

What is the rationale behind this behaviour?

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

f(1);
// => 1
// => 42

Perhaps this was a genuine mistake. Which section of the ECMAScript specification defines this behaviour?

like image 372
davidchambers Avatar asked May 06 '13 00:05

davidchambers


People also ask

What is mutating an object in JavaScript?

Non-primitive/Reference Data type — are Objects and Arrays. Reference types in JavaScript are mutable, meaning that the state and fields of mutable types can be changed. No new instance is created as a result.

How do you prevent the mutation of an argument to a function?

We can prevent mutation of objects and arrays using the Object. freeze() JavaScript function. We pass the desired object or array as an argument to this function, which later prevents any change to the object's or array's data.

What is the difference between a parameter and an argument JavaScript?

Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function.

Can objects be passed as parameters JavaScript?

In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.


1 Answers

Actually, in strict mode, this does not happen as you can see here.

If you read section 10.6 of the ECMA Standard, in particular Note 1, you'll see:

For non-strict mode functions the array index (defined in 15.4) named data properties of an arguments object whose numeric name values are less than 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.

In short, what this is saying is that, in non-strict mode, named function parameters operate as aliases for items in the arguments object. Thus, changing the value of a named parameter will change the value of the equivalent arguments item and vice versa. This is not a mistake. This is expected behaviour.

As an editorial, it's probably not a good idea to rely on this behaviour as it can lead to some very confusing code. Also, such code, if executed in strict mode, would not longer work.

like image 193
Dancrumb Avatar answered Oct 21 '22 19:10

Dancrumb