是一个函数返回需要被称为Closure [英] Is a function return necessary to be called a Closure
问题描述
我在YouTube上遇到这部影片 http://www.youtube.com/ watch?v = KRm-h6vcpxs
Hey i came across this video on youtube http://www.youtube.com/watch?v=KRm-h6vcpxs
这基本上解释了IIFE和闭包。但是我不知道我是否需要返回一个函数来调用它闭包。
which basically explains IIFEs and closures. But what I am not understanding is whether i need to return a function in order to call it a closure.
Ex
function a() {
var i = 10;
function b() {
alert(i);
}
}
在这种情况下,我可以将其称为闭包正在从外部函数的作用域访问'i'变量,或者我需要返回这样的函数
in this case can i call it a closure as it is accessing the 'i' variable from the outer function's scope or do i need to return the function like this
return function b(){alert(i);}
推荐答案
什么也不改变,重要的是创建它并调用它。这使得闭包,这是从内部函数到创建它的范围的链接(你可以看到它,在实践中,作为一个指针。它具有相同的效果,例如防止外部范围的混乱) 。
Returning the function changes nothing, what's important is creating it and calling it. That makes the closure, that is a link from the internal function to the scope where it was created (you can see it, in practice, as a pointer. It has the same effect of preventing the garbaging of the outer scope, for example).
这篇关于是一个函数返回需要被称为Closure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!