-1

I will use javascript as an example. I am confused because I have seen various answers as to what part "is" the closure. Is counterFunc the closure? Is incCount the closure? Is count the closure? I don't want to be at a job interview and point to the wrong part of the code and look like a poser : /

var counterFunc = function()
{
  var count = 0;

  var incCount = function()
  {
    count = count + 1;
    return count;
  };

  return incCount;
};

var myCounter = counterFunc();
console.log(myCounter());
console.log(myCounter());
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • 1
    See http://en.wikipedia.org/wiki/Closure_(computer_programming) and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures – gen_Eric Jun 25 '14 at 15:20
  • 2
    `isCount`/`myCounter` is the closure. `counterFunc` provides the closure scope, and `count` is a closure variable (it is "closed over") – Bergi Jun 25 '14 at 15:21
  • 1
    @Bergi that's inconsistent with the Wikipedia page, which in this case would say that a reference to an instance of the `count` variable is also part of the closure. – Alnitak Jun 25 '14 at 15:22

1 Answers1

1

The "closure" is not just the body of the inner function, but that function combined with whatever (internal) state that function needs to retain access to the outer-scoped variables to which it refers.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Since in JS the `[[scope]]` always is a property of the function object, I don't think this distinction is necessary – Bergi Jun 25 '14 at 15:22
  • @Bergi I wonder - does the `[[scope]]` only contain explicitly referenced variables from the outer scope, or could `eval` also provide access to every variable in the enclosing scopes. Anyhow, I've clarified that it's not really the _body_ of the function that's the closure. – Alnitak Jun 25 '14 at 15:29
  • Hmm, a test suggests it's the entire scope. – Alnitak Jun 25 '14 at 15:31
  • Language spec only says it references the entire scope. However, js implementations are [known to do optimisations](http://stackoverflow.com/a/8667141/1048572) and can garbage-collect any variables that are no more referenced (not in case of `eval`, of course) – Bergi Jun 25 '14 at 15:38
  • But you're right, it's good to emphasize that the closure is more than the code of the function – Bergi Jun 25 '14 at 15:40
  • @Bergi interesting link, and yet another reason to avoid `eval` ! – Alnitak Jun 25 '14 at 15:46
  • I am not sure why I was down-voted. I feel like this is a great question because a closure is an abstract thing and when I see it written in different languages the answer appears to vary to me. After reading your answer Alnitak, is the inner function and the free variables that the function refers to the closure? The outer function is the closure scope? Is that a precise analysis for the function in the question? – smuggledPancakes Jun 25 '14 at 17:24
  • 1
    @smuggledPancakes that's pretty close, but it's not just restricted to the outer function - that function itself may be enclosed in other functions, and there's the global scope too. – Alnitak Jun 25 '14 at 19:22