gpt4 book ai didi

javascript - Arguments 对象的 "unusual"特征

转载 作者:行者123 更新时间:2023-11-28 00:24:53 27 4
gpt4 key购买 nike

这是摘自“JavaScript,权威指南”中有关 arguments 对象的核心 JavaScript 引用的一段话:

In non-strict mode, the Arguments object has one very unusual feature. When a function has named arguments, the array elements of the Arguments object are synonyms for the local variables that hold the function arguments. The Arguments object and the argument names provide two different ways of referring to the same variable. Changing the value of an argument with an argument name changes the value that is retrieved through the Arguments object, and changing the value of an argument through the Arguments object changes the value that is retrieved by the argument name.

简单来说,这意味着什么?举个例子就太好了。

最佳答案

试试这个:

function x(a, b) {
arguments[1] = "foo";
console.log(b);
}

x("hello", "world");

您将在控制台中看到“foo”arguments 对象具有类似数组的属性,这些属性是函数声明的形式参数的别名。这意味着当您更改 arguments[0] 时,也会更改第一个显式声明的形式参数的值。 JavaScript 中没有其他方法可以给变量起别名,因此参数变得“不寻常”。

关于javascript - Arguments 对象的 "unusual"特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29642062/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com