实际上是一个闭包创建? [英] When actually is a closure created?

查看:99
本文介绍了实际上是一个闭包创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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. 情况1不是闭包,而只是一个函数

  2. 情况2不是闭包,而是带有自由变量的另一个函数

  3. case 3不是一个闭包,它是另一个特殊的变量 this 的函数。当函数被调用为对象的成员时,对象被赋值为 this 的值。否则, this 的值是全局对象。

  4. 情况4不是闭包,它是在另一个函数内定义的函数。如果 foo return bar ,您将创建一个仅包含bar及其值的闭包: function bar(){}


Is it true that a closure is created in the following cases for foo, but not for bar?

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, but bar is not a closure (yet), because the function foo is not invoked in the code, so no closure bar is ever created. It will only exist if foo is invoked , and the closure bar will exist until foo returns, and the closure bar 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 of foo. 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:

  1. Case 1 is not a closure, it's just a function
  2. Case 2 is not a closure, it's another function with a free variable
  3. 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 of this. Otherwise, the value of this is the global object.
  4. Case 4 is not a closure, it's a function defined inside another function. Should foo return bar, you would create a closure that contains only 'bar' and its value : function bar() {}.

这篇关于实际上是一个闭包创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆