gpt4 book ai didi

javascript - 封闭变量是什么意思?

转载 作者:行者123 更新时间:2023-11-29 15:38:35 24 4
gpt4 key购买 nike

<分区>

以下示例取自 a StackOverflow answer这有助于识别闭包。

for (var i = 0; i < 10; i++) {
(function f() {
var i2 = i;
setTimeout(function g() {
console.log(i2);
}, 1000);
})();
}

给出了以下解释:

For g:

List the variables:console is a free variable.i2 is a free variable.Find the parent scope to which each free variable is bound:console is bound to the global scope.i2 is bound to the scope of f.In which scope is the function referenced? The scope of setTimeout.Hence console is not closed over by g.Hence i2 is closed over by g.

但是我无法理解粗体部分 - 你能给我解释一下吗?

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