JavaScript:为什么更改参数变量会更改“arguments”;数组";?
考虑:JavaScript:为什么更改参数变量会更改“arguments”;数组";?,javascript,Javascript,考虑: > function hello(what) { . what = "world"; . return "Hello, " + arguments[0] + "!"; . } > hello("shazow") "Hello, world!" 为什么更改的值会更改参数[0]的值 “为什么更改what的值会更改参数[0]的值?” 因为它就是这样设计的。形式参数直接映射到arguments对象的索引 除非您处于严格模式,并且您的环境支持严格模式。那么更新一个不会
> function hello(what) {
. what = "world";
. return "Hello, " + arguments[0] + "!";
. }
> hello("shazow")
"Hello, world!"
为什么更改的值会更改参数[0]
的值
“为什么更改what
的值会更改参数[0]
的值?”
因为它就是这样设计的。形式参数直接映射到arguments对象的索引
除非您处于严格模式,并且您的环境支持严格模式。那么更新一个不会影响另一个
function hello(what) {
"use strict"; // <-- run the code in strict mode
what = "world";
return "Hello, " + arguments[0] + "!";
}
hello("shazow"); // "Hello, shazow!"
函数hello(什么){
“使用strict”;//哦……在这种情况下:为什么设计成这样?这种设计在任何地方都有记录吗?(当然不是说我不相信你,我只是想了解更多细节)哦,等等,我们开始:“注1”@DavidWolever的10.6参数对象中:语言是ECMAScript规范中的文档。ECMAScript 5中引入了严格模式。@DavidWolever为方便起见,在github上有一个。阅读量比官方规范轻一些: