最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript: why does changing an argument variable change the `arguments` "array"? - Stack Overflow

programmeradmin1浏览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]?

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]?

Share Improve this question asked Apr 19, 2012 at 2:50 David WoleverDavid Wolever 155k93 gold badges363 silver badges510 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 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!"
发布评论

评论列表(0)

  1. 暂无评论