Recursive closure in JavaScript
Asked Answered
B

2

6

enter image description here

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

Browder answered 23/12, 2016 at 7:48 Comment(3)
Is it really that hard to format and indent your code correctly? Anyway, please post a screenshot of the devtools display which is confusing you.Heptameter
I have added a scrrenschotBrowder
you just have references in both directions, there is no loop. :)Precedency
H
2

A closureis a special kind of object that combines two things: a function, and the environment in which that function was created.

  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 ); in your code it returns the Second function, i think it is clear for you.

  2. Now when you will expand this function it will show you in Closure the parent function (environment function) of Second, which is 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; and the 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. (same it did in step 2).

Now it repeats the step 3 again and then step 4. and so onnn... May be you understand what is happening here.

Hardball answered 23/12, 2016 at 9:25 Comment(15)
But i am not seeing any buildlist function in my closure??Browder
@Parshuram, Are you executing same code you posted in question?Hardball
Ya sir but just it was list variable,,instead of i that does not make any difference see my screenshot nowBrowder
But still i am not seeing my parent function in closureBrowder
Then it is showing right data. If you declare a local variable no matter it is 'i' or 'list', it will show you all variables and functions enclosed in environment except the function you returned.Hardball
Parshuram, can you tell me your exact requirement from these functions, may me i can help you more. :)Hardball
i am very much confused about this closure why this closure has its own closure reference, doesnt that increase javascript complexityBrowder
Let us continue this discussion in chat.Hardball
@BenAston for you, i think you need to take a look here. developer.mozilla.org/en/docs/Web/JavaScript/ClosuresHardball
@BenAston One more thing for you, https://mcmap.net/q/1916486/-everything-is-an-objectHardball
Please show me where any documentation says that closures are objects.Ferruginous
@BenAston I already gave you the link, i think u still didn't real this, go through this once please. developer.mozilla.org/en/docs/Web/JavaScript/ClosuresHardball
A closure is a mechanism that makes free variables available to inner functions. Does that make a closure an object - not IMO. Conceptually a closure is more abstract than an object. In the JS implementation the key components of a closure are a function, a ref to an outer lexical environment, the mechanism to configure that ref, a mechanism to navigate that ref and an implementation of a "lexical environment". So reified it is more than an object. Colloquially the important bit is the reference to the outer lexical environment. In no sense is a closure just a special object.Ferruginous
@BenAston A closure is a special kind of object that combines two things: a function, and the environment in which that function was created. The environment consists of any local variables that were in-scope at the time that the closure was created. In this case, a() is a closure that incorporates both the first(), second() functions and the variable 'i' that existed when the closure was created. If you are not accepting mozilla documentation (developer.mozilla.org/en/docs/Web/JavaScript/Closures) then i think you don't want to accept. Best of luck :)Hardball
@Manoj i creates a closure having first and i in it ,, not second because second itself creates a closure,,, ya and closures are special objectBrowder
A
0

The developer tools displays the variable a which is a variable that points to a anonymous function/closure.

In javascript a function is defined in a scope and also it can define a scope by its body block. 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). The function first is defined in the scope of the function a.

The variable first is also known in the scope of the anonymous function you assigned to the variable first.

And that what what you get on your screen: first is a variable containing a function. In the scope of this function a variable first is known that points to a function. In the scope of this function ...

You see?

Anteater answered 23/12, 2016 at 9:10 Comment(2)
If you say first has also its own scope,,then why second is not having its own scope,,,??? it only has first and list scope??Browder
Yes, it seams that the chrome-debugger only shows variables in a scope, which are used in this scope. See the code-text in the 3rd line of your screenshot output.Anteater

© 2022 - 2024 — McMap. All rights reserved.