O'Reilly logo
  • Ahmed Elgendy thinks this is interesting:

function makeCounter() { var count = 0; function counter() { count = count + 1; return count; } return counter; } var doCount = makeCounter(); console.log(doCount());


Cover of Head First JavaScript Programming


Why am I assigning a function invocation to a reference variable here?
A tip: because the invoked function is supposed to return a function. But why you did not call that function directly. Because the inner function is nested and it is not global so in order to execute the outer function (the enclosing function) which is supposed to return a reference to the inner function, I need to assign the returned function to some global variable to be able to execute it.