实际上是一个闭包创建? [英] When actually is a closure created?
问题描述
在 foo
,但不是 bar
下面的情况下创建闭包是真的吗? / p>
案例1:
< script type =text / javascript >
function foo(){}
< / script>
foo
情况2:
< script type =text / javascript>
将会存在到
var i = 1;
function foo(){return i; }
< / script>
3:< script type =text / javascript>
function Circle(r){
this.r = r;
}
Circle.prototype.foo = function(){return 3.1415 * this.r * this.r}
< / script>
在这种情况下,
Circle.prototype.foo
(返回圆的面积)指的是只有全局范围的闭包。 (创建此关闭)。
案例4:
; script type =text / javascript>
function foo(){
function bar(){
}
}
< / script>
这里,
foo
全局范围,但bar
不是一个闭包(还),因为函数foo
不在代码中调用,因此没有创建任何闭包bar
。它只会在调用foo
时才存在,并且bar foo
返回,然后闭包bar
将被垃圾回收,因为在任何地方都没有引用它。
所以当函数不存在时,不能被调用,不能被引用,然后闭包不存在(从未创建)。
解决方案只有当函数可以被调用或被引用时,在某些功能代码中,通过函数context(关闭是比上下文更合适的术语)绑定到一些值。
< script type =text / javascript>
var i = 1;
function foo(){return i; }
< / script>
这里,
i
函数代码foo
。并且这个自由变量不被任何现有上下文(闭包)绑定到任何特定值。因此,您没有任何关闭。< script type =text / javascript
var i = 1;
function foo(){return i; }
foo(); // returns 1
i = 2;
foo(); // returns 2
< / script>
现在要创建一个闭包,你必须提供一个有价值的上下文:
< script type =text / javascript>
function bar(){
var i = 1;
function foo(){return i; }
return foo;
}
bar(); // returns function foo(){return i; }
bar()(); //返回1
//没有办法改变自由变量的值i => bound => closure
< / script>
总之,除非函数返回另一个函数,否则不能有闭包。在这种情况下,返回的函数具有退出时返回函数中存在的所有变量值绑定。
< script type =text / javascript>
function bar(){
var i = 1;
function foo(){return i; }
i = 2;
return foo;
}
bar()(); // returns 2
< / script>
关于您的示例:
- 情况1不是闭包,而只是一个函数
- 情况2不是闭包,而是带有自由变量的另一个函数
- case 3不是一个闭包,它是另一个特殊的变量
this
的函数。当函数被调用为对象的成员时,对象被赋值为this
的值。否则,this
的值是全局对象。
- 情况4不是闭包,它是在另一个函数内定义的函数。如果
foo
returnbar
,您将创建一个仅包含bar及其值的闭包:function bar(){}
。
Is it true that a closure is created in the following cases for
foo
, but not forbar
?Case 1:
<script type="text/javascript"> function foo() { } </script>
foo
is a closure with a scope chain with only the global scope.Case 2:
<script type="text/javascript"> var i = 1; function foo() { return i; } </script>
same as Case 1.
Case 3:
<script type="text/javascript"> function Circle(r) { this.r = r; } Circle.prototype.foo = function() { return 3.1415 * this.r * this.r } </script>
in this case,
Circle.prototype.foo
(which returns the circle's area) refers to a closure with only the global scope. (this closure is created).Case 4:
<script type="text/javascript"> function foo() { function bar() { } } </script>
here,
foo
is a closure with only the global scope, butbar
is not a closure (yet), because the functionfoo
is not invoked in the code, so no closurebar
is ever created. It will only exist iffoo
is invoked , and the closurebar
will exist untilfoo
returns, and the closurebar
will then be garbage collected, since there is no reference to it at all anywhere.So when the function doesn't exist, can't be invoked, can't be referenced, then the closure doesn't exist yet (never created yet). Only when the function can be invoked or can be referenced, then the closure is actually created?
解决方案A closure is when free variables in some function code are bound to some values by the function "context" (closure being a more proper term here than context).
<script type="text/javascript"> var i = 1; function foo() { return i; } </script>
Here,
i
is a free variable for the function code offoo
. And this free variable is not bound to any particular value by any existing context (closure). So you don't have any closure.<script type="text/javascript"> var i = 1; function foo() { return i; } foo(); // returns 1 i = 2; foo(); // returns 2 </script>
Now to create a closure you have to provide a value-bounding context:
<script type="text/javascript"> function bar() { var i = 1; function foo() { return i; } return foo; } bar(); // returns function foo() { return i; } bar()(); // returns 1 // no way to change the value of the free variable i => bound => closure </script>
In summary, you can't have a closure unless a function returns another function. In that case, the returned function has all the variable-value bindings that existed in the returning function when it exited.
<script type="text/javascript"> function bar() { var i = 1; function foo() { return i; } i = 2; return foo; } bar()(); // returns 2 </script>
Concerning your exemples:
- Case 1 is not a closure, it's just a function
- Case 2 is not a closure, it's another function with a free variable
- Case 3 is not a closure, it's yet another function with the special "variable"
this
. When the function is called as member of an object, the object is assigned to the value ofthis
. Otherwise, the value ofthis
is the global object.- Case 4 is not a closure, it's a function defined inside another function. Should
foo
returnbar
, you would create a closure that contains only 'bar' and its value :function bar() {}
.
这篇关于实际上是一个闭包创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!