If we have this basic function (and its closures):
function counter(){
var n = 0;
return {
count: function() {return n++},
reset: function() {n = 0}
};
}
Is this what's happening in memory? (basically a pointer to a function object and its scope chain)
(source: geraldleroy.com)
Is the above diagram correct? If so, I'm not understanding why these two lines of code create two new scope chains and new private variables:
var c = counter();
var d = counter();
It seems like both c and d would refer to the original function object and would use its scope chain.
I'm a little confused and would appreciate any insight on this that anyone can offer.
Thanks!