 For a mental model, pretend that variables are referring to storage locations in an array and you reference them using indices - v = [..., v, v, v, v, ....]For example, take the function in the stackoverflow answer -`````` function foo(x) { var tmp = 3; return function (y) { alert(x + y + (++tmp)); } } `````` When you make a call to foo(10), pretend that the bound variables in the body of the function get assigned sequence numbers starting from 100 -`````` function foo() { v = 10; // x = 10 v = 3; // var tmp = 3; return function (y) { alert(v + y + (++(v))); } } `````` So you get as the result,`````` function (y) { alert(v + y + (++(v))); } `````` 'y' remains as such because it hasn't been bound to a value just yet. That will happen only when you call this result function.Now, when you call foo another time like foo(20), the sequence continues from 102 ...`````` function foo() { v = 20; // x = 20 v = 3; // var tmp = 3; return function (y) { alert(v + y + (++(v))); } } `````` So you get another function as the result -`````` function (y) { alert(v + y + (++(v))); } `````` The store now reads v = 10, v = 3, v = 20 and v = 3.It becomes clear what the two result functions do. Note that they do not share the same storage locations and therefore the two ++ calls increment different storage locations.In this model, each "var" statement and each argument of a function causes the index assigned to be incremented on a function call, and unbound variables do not cause increments. The behaviour of closures created in javascript is as though such an indefinitely increasing numbering scheme is being used under the hood.(edited a bit for clarity). 'y' remains as such because it hasn't been bound to a value just yet.That's easy enough to test. var y=20;`````` function foo(x) { var tmp = 3; var y = 20; return function (y) { alert(x + y + (++tmp)); }`````` }var bar = foo(2); // bar is now a closure.bar(10);This makes no difference. The alert runs once, and displays 16. Applications are open for YC Summer 2019

Search: