简体   繁体   English

JavaScript 中的递归闭包

[英]Recursive closure in JavaScript

在此处输入图片说明

function buildList( list ) {
  var i      = 0;
  var first  = function () {
    console.log( "in" )
    console.log( i );
  }
  var Second = function () {
    console.log( "out" )
    first();
  }
  return Second;
}

var a = buildList( [1, 2, 3] )
console.dir( a );

a(); // Here closure is created which has function first ,Here first also has one closure of itself that means recursive closure

When i see my console in Chrome it has a closure which has function first which also has a closure of itself ie it has repetitive loop of its own function in closure, Does anyone knows whats happening here, I am very much confused, Why there is infinte closure loop当我在 Chrome 中看到我的控制台时,它有一个闭包,它首先有一个函数,它本身也有一个闭包,即它在闭包中有自己函数的重复循环,有谁知道这里发生了什么,我很困惑,为什么有无限闭环

A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. closure是一种特殊的对象,它结合了两件事:函数和创建该函数的环境。

  1. No need to be confused, the behavior is same as expected to this code.无需混淆,行为与此代码的预期相同。 Here what happening is that when you do console.dir( a );这里发生的事情是当你做console.dir( a ); in your code it returns the Second function, i think it is clear for you.在您的代码中,它返回Second函数,我认为您很清楚。

  2. Now when you will expand this function it will show you in Closure the parent function ( environment function ) of Second , which is buildList .现在,当您展开此函数时,它将在Closure显示Second的父函数( environment function ),即buildList In you code it is doing the same thing.在您的代码中,它正在做同样的事情。

  3. Now next thing is to expand this function buildList , what it will show you is the child objects of it, Which are var i = 0;现在下一步是扩展这个function buildList ,它会向你展示它的子对象,它们是var i = 0; and the function first .function first Your console is showing as expected.您的控制台按预期显示。

  4. Now again when you open first() it will show you in Closure the parent function( environment function ) of first , which is buildList .现在,当你再次打开first()它会告诉你在Closure父功能( environment function )的first ,这是buildList (same it did in step 2). (与第 2 步中的操作相同)。

Now it repeats the step 3 again and then step 4. and so onnn... May be you understand what is happening here.现在它再次重复第 3 步,然后是第 4 步。依此类推……也许你明白这里发生了什么。

The developer tools displays the variable a which is a variable that points to a anonymous function/closure.开发人员工具显示变量 a,它是一个指向匿名函数/闭包的变量。

In javascript a function is defined in a scope and also it can define a scope by its body block.在 javascript 中,一个函数定义在一个作用域中,它也可以通过它的主体块定义一个作用域。 A scope "knows" all variables inside the defining block and all variables that are defined outside of the function but in the hierachy of scopes the scope is defined in.作用域“知道”定义块内的所有变量以及在函数外部定义但在作用域的层次结构中定义的所有变量。

The tools show you the scope of the returned function ( a in this case).该工具,您可以返回功能的(范围a在这种情况下)。 The function first is defined in the scope of the function a .函数first在函数a的范围内定义。

The variable first is also known in the scope of the anonymous function you assigned to the variable first .变量first在您分配给变量first的匿名函数的范围内也是已知的。

And that what what you get on your screen: first is a variable containing a function.这就是你在屏幕上看到的: first是一个包含函数的变量。 In the scope of this function a variable first is known that points to a function.在这个函数的范围内, first知道一个指向函数的变量。 In the scope of this function ...在此功能范围内...

You see?你看?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM