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?