Within instances of objects I like to use the closure mechanism to simulate private member variables. For a potential large number of created objects I don’t need some of the private members though, but I have to declare them for them to be used in the closure, like “one”, “two” and “three” here:
var obj=function()
{
var one;
var two;
var three;
var M=function()
{
one=5;
};
};
(Don’t mind that this is not actually a working example of my setup, it’s just to demonstrate the use of the closure over the three vars with M.)
Do the var statements themselves already consume memory, or does that depend on actually assigning something to these vars like with “one”?
The interpreter has to store information about scope –
one = 5will change the local variableoneinstead of creating a global variable (which would happen with e.g.four = 5). This information must cost some memory somehow. This memory usage also applies before assigning a value toone, because the information has to be available at the time you’re assigning.How much memory it will cost is difficult to say since it differs per interpreter. I guess it isn’t enough to worry about.
Note that
two/threeare not used at all and may be garbage collected in this example. (Actually, you don’t exposeMeither, so everything may be garbage collected right away in this example.)