JavaScript:为什么更改参数变量会更改`arguments`"数组"?

Dav*_*ver 12 javascript

考虑:

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

为什么改变价值what变化的价值arguments[0]

小智 13

"为什么改变价值会what改变价值arguments[0]?"

因为这是它的设计工作方式.形式参数直接映射到arguments对象的索引.

也就是说,除非你是严格模式,和你的环境支持它.然后更新一个不会影响另一个.

function hello(what) {
    "use strict"; // <-- run the code in strict mode
    what = "world";
    return "Hello, " + arguments[0] + "!";
}
hello("shazow"); // "Hello, shazow!"
Run Code Online (Sandbox Code Playgroud)