by kylec · 13y ago · 0 comments
var counter = (function () {
var i = 0;
return function () {
i += 1;
return i;
}
}());
Where the internal variable, i, is inaccessible to anything but the counter function itself.if your function doesn't return another function, the references from that function are deallocated once the function has executed.
when your function returns a function (a "closure"), the inner function closes over the variables of the outer function, maintaining references to it's context as long as you maintain a reference to the closure.