how does memory stacks work in javascript

Posted by user227353 on Stack Overflow See other posts from Stack Overflow or by user227353
Published on 2010-04-20T18:33:40Z Indexed on 2010/04/20 18:43 UTC
Read the original article Hit count: 194

Filed under:
|
|

When we have code like:

function a(){
  var x =0;
  this.add=function(){
    alert(x++);
  }
}

   var test = new a();
   test.add(); // alert 0
   test.add(); // alert 1
   test.add(); // alert 2

How does this work? Doesn't that the value of 'x' in a() should be 'gone' as soon as test = new a() is complete? The stack contains x should also be gone as well, right? Or, does javascript always keep all the stacks ever created in case they will be referenced in future? But that wouldn't be nice, would it...?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about memory