1

In this accepted popular answer the closures are explained. I understand the concept and can use it, but my question is rather terminological, look at this simple example:

function foo(x) {
  var tmp = 3;

  function bar(y) {
    alert(x + y + (++tmp)); // will alert 16
  }

  bar(10);
}

foo(2);

So what exactly should be called a closure here: bar? or foo? or tmp? Or all of them together?

You see how community wiki is calling bar a closure, is that exactly right? Then how do I refer in a conversation to tmp and to foo? Are they also called closure, or variable inside a closure and closure accessor or whatever else? Or bar is called external closure and foo is internal closure?

Community
  • 1
  • 1
exebook
  • 32,014
  • 33
  • 141
  • 226

1 Answers1

0

Bar is a closure as it has closed environment in which tmp value exist which is local variable of foo function. Local or private variable can be accesed by inner functions even after the function has return because of the closed environment formed (In which value of local variable of outer function resides), that's why it is called closure, So here Bar is termed as closure but it closes the Environment of Foo.

A.B
  • 20,110
  • 3
  • 37
  • 71