Search This Blog

Saturday, February 12, 2011

Enclosure Memory Copies

Programmer Question

function myClass()
{
//lots and lots of vars and code here.

this.bar = function()
{
//do something with the numerous enclosed myClass vars
}

this.foo = function()
{
alert('Hello'); //don't do anything with the enclosed variables.
}
}


Each instance of myClass gets its own copy of bar and foo, which is why prototyped methods use less memory. However, I would like to know more about the memory use of inner methods.



It seems obvious to me that (1) below must be true. Do you agree?




  1. Not only do distinct myClass instances have their own distinct copies of bar, but they also must have their own distinct copies of the myClass enclosure. (or else how does the bar method keep the myClass variables straight on a per instance basis?)



Now (2) is the question I'm really after.




  1. Since the inner foo method doesn't use anything in the myClass enclosure a natural qustion is: Is javascript smart enough to not keep a myClass enclosure in memory for the use of foo?



Find the answer here

No comments:

Post a Comment

Related Posts with Thumbnails