gpt4 book ai didi

javascript - 参数和引用

转载 作者:数据小太阳 更新时间:2023-10-29 04:13:35 26 4
gpt4 key购买 nike

考虑这个 JavaScript 函数:

var f = function (a) {
console.log(a+" "+arguments[0]);
a = 3;
console.log(a+" "+arguments[0]);
}

我希望 aarguments[0] 仅在函数的第二条语句之前引用相同的值。相反,它们似乎总是引用相同的值:f(2) causes

2 2
3 3

f({foo: 'bar'}) 原因:

[object Object] [object Object]
3 3

参数标识符和 arguments 标识符是否以特殊方式链接?

最佳答案

Are argument identifiers and the arguments identifier linked in a special way?

是的(但仅限于非严格模式)。

来自规范(ES6ES5):

For non-strict mode functions the integer indexed data properties of an arguments object whose numeric name values are less than the number of formal parameters of the corresponding function object initially share their values with the corresponding argument bindings in the function’s execution context. This means that changing the property changes the corresponding value of the argument binding and vice-versa. This correspondence is broken if such a property is deleted and then redefined or if the property is changed into an accessor property. For strict mode functions, the values of the arguments object’s properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.

关于javascript - 参数和引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815033/

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