0

So I understand what a Closure in JavaScript does, but I don't understand "how" it remembers.

Using the YDKJS book's example:

function foo() {
    var a = 2;

    function bar() {
        console.log( a );
    }

    return bar;
}

var baz = foo();

baz()//Prints 2

I get that bar is being returned, and bar has access to the lexical scope which includes a. We could make multiple items using foo()'s return value which is great! However what exactly is happening behind the scenes.

Is a reference to the original variable getting returned with the function....or some copy that's hidden? What's actually going on?

msmith1114
  • 2,717
  • 3
  • 33
  • 84
  • 4
    `but I don't understand "how" it remembers` - is the `how` important? here's some [documentation](http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/#ecmascript-closures-implementation) to satisfy your curiosity – Jaromanda X May 17 '17 at 02:25
  • 2
    http://stackoverflow.com/questions/33802718/closures-and-es2015 http://stackoverflow.com/questions/2800463/how-variables-are-allocated-memory-in-javascript http://stackoverflow.com/questions/15117687/javascript-closures-using-the-ecma-spec-please-explain-how-the-closure-is-created http://mrale.ph/blog/2012/09/23/grokking-v8-closures-for-fun.html – Bergi May 17 '17 at 02:40
  • 1
    "*Is a reference to the original variable getting returned with the function?*" - Yes, that's almost literally the [definition of "closure"](https://en.wikipedia.org/wiki/Closure_(computer_programming)) – Bergi May 17 '17 at 02:42
  • @Bergi Is the process for installing `v8` presently the same as described at link? – guest271314 May 17 '17 at 04:22
  • @guest271314 I don't know, I never tried it, but surely you can find out with a web search – Bergi May 17 '17 at 04:29

3 Answers3

4

Is a reference to the original variable getting returned with the function....or some copy that's hidden? What's actually going on?

Essentially, yes. The returned function carries along with it its enclosing scope, which includes the variables in that scope. It's not a copy of those variables--it's the scope itself. That is the essence of closures.

0

The variable a is still being referenced by function bar.

guest271314
  • 1
  • 15
  • 104
  • 177
0

To understand closures, you need to understand nested scopes. Each function has its own scope think of it as its private space where it can access outside but the outside of the function can not access back the private scope of the function. when foo is being executed the processor try to look for a variable named "a" in the local scope of foo, if it finds one great, it uses it; if not, it looks up the chain for a variable named a, and so on till it hits the global scope. In this example the variable "a" is found in the upper function, and it's used Notice that even if you set

var a =3;

in the global scope, bar function execution would still print 2,

user10089632
  • 5,216
  • 1
  • 26
  • 34
  • This is not quite right. It implies that the lookup is based on the runtime state of each function object. It is _statically_ scoped. What you have described is basically the `with` statement. The problem is that you say _when_ the function is executed. Perhaps that is not what you meant but it is confusing to say the least given how prototypes work. – Aluan Haddad May 17 '17 at 02:40
  • @AluanHaddad Doing the lookup statically is only an optimisation, it leads to the same results. And as you observed, it's not even always possible, e.g. when involving object records or `eval`. – Bergi May 17 '17 at 02:43
  • I mean that there is no state-dependent traversal of the scope chain. There is a mapping from `bar` to a symbol `a` as defined in some outer scope. Which outer scope that is is fixed when bar `is` created. – Aluan Haddad May 17 '17 at 02:47
  • 1
    @AluanHaddad as my first sentence suggest, I was describing the lookup chain, mentioning execution does not necessarily means execution context as of being different from the scope, it's a way to explain step by step, the algorithm used to determine the value of a variable in a function. If that still seem to be wrong please enlighten me – user10089632 May 17 '17 at 04:03