Jack
Jack

Reputation: 5870

Confused about Node js two brackets function

I'm trying to understand the recursive function in NodeJS, but I'm still confused about the following code and output:

var firstf = function () {
    var counter = 0;
    return function () {    
           console.log("counter = " + counter);
           return counter += 1;
           }
    };
var add = firstf();
add();//output 0
add();//output 1
add();//output 2
firstf()();//output 0
firstf()();//output 0
firstf()();//output 0

I can understand three add() functions output 0,1,2, but I could not understand why three firstf()() output 0,0,0. what does two ()() mean please?

Also one follow up question: for this line: var add = firstf(); the variable add will represents the return function as:

function () {    
  console.log("counter = " + counter);
  return counter += 1;
}

Ok, the question is that how could this function see the variable counter, since counter in upper level, not defined in this inner function.

Upvotes: 2

Views: 70

Answers (1)

jfriend00
jfriend00

Reputation: 707218

There is no recursion here. Recursion is where a function calls itself. This is called a closure. Because the inner function contains references to variables in the outer function's scope. Here's a good article on closures. From that article:

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).

In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time. To use a closure, define a function inside another function and expose it.

To expose a function, return it or pass it to another function. The inner function will have access to the variables in the outer function scope, even after the outer function has returned.


Now, let's diagnose exactly what is happening with firstf()().

First that calls firstf(). That initializes the internal counter to 0 and returns a new function.

Then, the second () executes that returned function which returns the value of counter which is 0 and increments it.

Then, you call firstf()() again. That initializes a new counter variable to 0 and returns a new function. Then, the second () calls that function and returns the new counter value of 0 and then increments it.

So, that explains why successive calls to firstf()() just keep returning 0. You keep making a new function and a new counter variable each time.

When you do var add = firstf(); and then call add(), you are storing the returned function and then calling the same function over and over again. That will keep using the same internal counter variable and thus you will see the returned value going up as that internal counter variable is incremented each time.

what does two ()() mean please?

Each () attempts to execute a function. In firstf()(), the first () executes firstf() and gets the function that it returns. The second () then executes that returned function and gets whatever it returns (the counter value).

Upvotes: 7

Related Questions