JavaScript:为什么更改参数变量会更改`arguments`" array"? [英] JavaScript: why does changing an argument variable change the `arguments` "array"?

查看:57
本文介绍了JavaScript:为什么更改参数变量会更改`arguments`" array"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

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

为什么更改的值更改 arguments的值[0]

推荐答案


为什么更改的值更改 arguments [0] 的值?

"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!"

这篇关于JavaScript:为什么更改参数变量会更改`arguments`&quot; array&quot;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆