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]
?
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
Reset to default 13"Why does changing the value of
what
change the value ofarguments[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!"