1

This is a quote from the Core JavaScript Reference in "JavaScript, the Definitive Guide" regarding the arguments object:

In non-strict mode, the Arguments object has one very unusual feature. When a function has named arguments, the array elements of the Arguments object are synonyms for the local variables that hold the function arguments. The Arguments object and the argument names provide two different ways of referring to the same variable. Changing the value of an argument with an argument name changes the value that is retrieved through the Arguments object, and changing the value of an argument through the Arguments object changes the value that is retrieved by the argument name.

In simple terms, what does this mean? An example would be great.

1 Answer 1

1

Try this:

function x(a, b) {
  arguments[1] = "foo";
  console.log(b);
}

x("hello", "world");

You'll see "foo" in the console. The arguments object has array-like properties that alias the formal parameters declared by the function. That means that when you change arguments[0], that also changes the value of the first explicitly-declared formal parameter. There's no other way in JavaScript to alias variables, so that makes arguments "unusual".

4
  • What's that got to do with being in strict mode or not? The console outputs "foo" in both cases.
    – shmuli
    Commented Apr 15, 2015 at 5:31
  • 1
    @shmuli: with "use strict" it logs "world" in my copy of Chrome... same goes for b = "foo"; console.log( arguments[1]);
    – dandavis
    Commented Apr 15, 2015 at 5:32
  • @shmuli good question - I haven't seen anything about "strict" mode changing that aspect of the arguments object behavior. Understand that things are in flux, so perhaps that was a thing that was once contemplated but later changed. edit - How did you test the strict version?
    – Pointy
    Commented Apr 15, 2015 at 5:32
  • @dandavis you're right; it works. I accidentally dropped a typo in there.
    – shmuli
    Commented Apr 15, 2015 at 5:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.