This is code that creates a local object inside a closure.
function A() {
this.welcome = "Welcome";
}
var P = (function() {
var a = new A();
function printa() {
console.log(a);
}
return {
printa: printa
};
})();
P.printa();
It prints:
{ welcome: 'Welcome' }
After the P=… function is executed, P gets a new object that holds a reference to a function printa(), but at this time, local variable a should be freed. Why is it not freed?
Or is it temporarily there because it’s not garbage collected?
ais kept referenced by the closure from theprintafunction.awill be garbage collected some time after there are no more references to theprintafunction.