Lifetime of javacript variables.
Posted
by The Machine
on Stack Overflow
See other posts from Stack Overflow
or by The Machine
Published on 2010-04-22T11:55:02Z
Indexed on
2010/04/22
12:13 UTC
Read the original article
Hit count: 264
What is the lifetime of a variable in javascript, declared with "var". I am sure, it is definitely not according to expectation.
<script>
function(){
var a;
var fun=function(){
// a is accessed and modified
}
}();
</script>
Here how and when does javascript garbage collect the variable a? Since 'a' is a part of the closure of the inner function, it ideally should never get garbage collected, since the inner function 'fun', may be passed as a reference to an external context.So 'fun' should still be able to access 'a', from the external context.
If my understanding is correct, how does garbage collection happen then, and how does it ensure to have enough memory space, since keeping all variables in memory until the execution of the program might not be tenable ?
© Stack Overflow or respective owner