Recursive closure in JavaScript

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.