gpt4 book ai didi

javascript - 为什么在第二次 fn 函数调用期间输出为 2?

转载 作者:行者123 更新时间:2023-11-28 04:07:21 25 4
gpt4 key购买 nike

我遇到了一个 Javascript 示例,但对其理由不太满意,或者对函数内部 this 的范围感到困惑。请参阅下面的示例:

var length = 10;
function fn() {
console.log(this.length);
}

var obj = {
length: 5,
method: function(fn) {
fn();
arguments[0]();
}
};

obj.method(fn, 1);

第一次记录10,这是可以理解的,因为它是从全局范围调用的。然而,第二次记录 2 这让我感到困惑。我寻找答案,这就是我得到的 -

We know that we can access any number of arguments in a JavaScript function using the arguments[] array.

Hence arguments0 is nothing but calling fn(). Inside fn now, the scope of this function becomes the arguments array, and logging the length of arguments[] will return 2

有人对此有更好的答案吗?

最佳答案

您调用arguments[0]();

因此,this 的值与arguments 相同。

参数为:fn, 1

有 2 个。

arguments.length 因此为 2

关于javascript - 为什么在第二次 fn 函数调用期间输出为 2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585926/

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