12

Consider:

> function hello(what) {
.     what = "world";
.     return "Hello, " + arguments[0] + "!";
. }
> hello("shazow")
"Hello, world!"

Why does changing the value of what change the value of arguments[0]?

1 Answer 1

13

"Why does changing the value of what change the value of arguments[0]?"

Because that's how it's designed to work. The formal parameters are directly mapped to the indices of the arguments object.

That is unless you're in strict mode, and your environment supports it. Then updating one doesn't effect the other.

function hello(what) {
    "use strict"; // <-- run the code in strict mode
    what = "world";
    return "Hello, " + arguments[0] + "!";
}
hello("shazow"); // "Hello, shazow!"
Sign up to request clarification or add additional context in comments.

8 Comments

Oh… In that case: why is it designed to work that way? And is that design documented anywhere? (not that I don't trust you, of course, I'd just like a bit more detail)
Oh, wait, here we go: "NOTE 1" in 10.6 Arguments Object of ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
@DavidWolever: The language is document in the ECMAScript specification. Strict mode was introduced in ECMAScript 5.
@DavidWolever—for convenience, there is an HTML version of ES5 on github.
A little lighter reading than the official spec: developer.mozilla.org/en/JavaScript/Reference/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.