IIFE 作为闭包
IIFEs as closures
在 You Don't Know Javascript 系列中,1/3 的 IIFE 被描述为本身不是闭包,但前提是它们在词法范围之外执行:
Chapter 3 introduced the IIFE pattern. While it is often said that
IIFE (alone) is an example of observed closure, I would somewhat
disagree, by our definition above.
This code "works", but it's not strictly an observation of closure.
Why? Because the function (which we named "IIFE" here) is not executed
outside its lexical scope. It's still invoked right there in the same
scope as it was declared (the enclosing/global scope that also holds
a). a is found via normal lexical scope look-up, not really via
closure.
var a = 2;
(function IIFE(){ // not actually a "closure"
console.log( a );
})();
在此 SO post 中,给出了以下代码片段作为闭包示例:
for (var i = 0; i < someVar.length; i++)
(function (i) {
window.setTimeout(function () {
alert("Value of i was "+i+" when this timer was set" )
}, 10000);
})(i);
我试图根据闭包的定义来理解这一点(如 this medium article 中所定义):
To use a closure, simply define a function inside another function and
expose it. To expose a function, return it or pass it to another
function. ...
The inner function will have access to the variables in the outer
function scope, even after the outer function has returned.
我知道闭包是 "stateful function",它是
a way to "remember" and continue to access a function's scope (its
variables) even once the function has finished running.
所以在这个例子中,我看到循环的 i
在传递到结束 IIFE 时被记住了。
我的问题是:
"passing to another function or returning" 部分发生在哪里?我的猜测是 IIFE 能够在每次迭代时记住外部 i
for 循环值,因为 IIFE 被传递给 window?
基本上,我的理解是闭包被定义为在垃圾收集器清理外部作用域后记住外部作用域的值,并且用法 是通过返回闭包并在其词法范围之外访问它来公开闭包。这个对吗?那么 "accessing it outside the lexical scope" 发生在哪里?
in this example, I see that the loop's i
is remembered when passed into the closing IIFE
没有。 IIFE 仅提供要记住的 i
值的范围。正如您引用的引号所述,IIFE 函数不是闭包。 使用的函数表达式i
是闭包:
(function IIFE(i) {
// this is the scope of i
window.setTimeout(function closure() {
// this function closes over i
alert("Value of my local 'i' is still "+i+" even after 10 seconds");
}, 10000);
})(0);
// ^ this is some arbitrary value passed to the IIFE - it could have been a loop variable
Where is the "passing to another function or returning" portion happening?
这是传递给 setTimeout
的 closure
函数,它将从通常不再定义 i
的地方回调它 - 如果它不是闭包.
在 You Don't Know Javascript 系列中,1/3 的 IIFE 被描述为本身不是闭包,但前提是它们在词法范围之外执行:
Chapter 3 introduced the IIFE pattern. While it is often said that IIFE (alone) is an example of observed closure, I would somewhat disagree, by our definition above.
This code "works", but it's not strictly an observation of closure. Why? Because the function (which we named "IIFE" here) is not executed outside its lexical scope. It's still invoked right there in the same scope as it was declared (the enclosing/global scope that also holds a). a is found via normal lexical scope look-up, not really via closure.
var a = 2;
(function IIFE(){ // not actually a "closure"
console.log( a );
})();
在此 SO post 中,给出了以下代码片段作为闭包示例:
for (var i = 0; i < someVar.length; i++)
(function (i) {
window.setTimeout(function () {
alert("Value of i was "+i+" when this timer was set" )
}, 10000);
})(i);
我试图根据闭包的定义来理解这一点(如 this medium article 中所定义):
To use a closure, simply define a function inside another function and expose it. To expose a function, return it or pass it to another function. ...
The inner function will have access to the variables in the outer function scope, even after the outer function has returned.
我知道闭包是 "stateful function",它是
a way to "remember" and continue to access a function's scope (its variables) even once the function has finished running.
所以在这个例子中,我看到循环的 i
在传递到结束 IIFE 时被记住了。
我的问题是:
"passing to another function or returning" 部分发生在哪里?我的猜测是 IIFE 能够在每次迭代时记住外部 i
for 循环值,因为 IIFE 被传递给 window?
基本上,我的理解是闭包被定义为在垃圾收集器清理外部作用域后记住外部作用域的值,并且用法 是通过返回闭包并在其词法范围之外访问它来公开闭包。这个对吗?那么 "accessing it outside the lexical scope" 发生在哪里?
in this example, I see that the loop's
i
is remembered when passed into the closing IIFE
没有。 IIFE 仅提供要记住的 i
值的范围。正如您引用的引号所述,IIFE 函数不是闭包。 使用的函数表达式i
是闭包:
(function IIFE(i) {
// this is the scope of i
window.setTimeout(function closure() {
// this function closes over i
alert("Value of my local 'i' is still "+i+" even after 10 seconds");
}, 10000);
})(0);
// ^ this is some arbitrary value passed to the IIFE - it could have been a loop variable
Where is the "passing to another function or returning" portion happening?
这是传递给 setTimeout
的 closure
函数,它将从通常不再定义 i
的地方回调它 - 如果它不是闭包.