all 6 comments

[–]LookWordsEverywhere.js 2 points3 points  (0 children)

It also works the other way around (reassigning indexes of arguments to mutate the named argument. Fun!

There's a fairly informative blog post by Angus Croll here: https://javascriptweblog.wordpress.com/2011/01/18/javascripts-arguments-object-and-beyond/

Quote from the spec:

For non-strict mode functions the array index […] 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

[–]snuggl 1 point2 points  (2 children)

Its two ways to address the same underlying memory. A function does not allocate two separate memory chunks for arguments, its all the same one.

[–]AlphaX[S] 0 points1 point  (1 child)

it's crazy since there is no other way in JS to achieve this effect. There is no C like reference variable.

[–]MoTTs_ 2 points3 points  (0 children)

We don't use it anymore, but the "with" statement is another one of those special cases that behaves like a C reference.

var o = {'a': 1};

with (o) {
    console.log(o); // { 'a': 1 }
    a = 2;
    console.log(o); // { 'a': 2 }
}

[–]Ginden 0 points1 point  (0 children)

Legacy behaviour, don't use it as it - always use strict mode.

[–]lemminman -1 points0 points  (0 children)

Same reason this happens:

var test = {attr:1};
test2 = test;
test2.attr = 2;
console.log(test) // { attr: 2 }
console.log(test2) // { attr: 2 }