Wednesday, September 18, 2013

Does Javascript closure always retain scope variables?

Does Javascript closure always retain scope variables?

I understand that code in closure can access variables & methods &
arguments up the scope chain, but what happens if it doesn't use any of
them ? do those variables still retained ?
Consider this case :
function f(){
var a=[];
for(var i=0;i<1000000;i++) a.push({});
return function(){
alert('Hi');
};
}
var x = f();
Does variable a retained in memory even though the closure does not use it ?
Thanks

No comments:

Post a Comment