There's something that's been bugging me about JavaScript closures. Variables created with the var
keyword still exist in memory once the constructor has completed its execution. Example:
function RangeCalculator(values) {
var min = Infinity;
var max = -Infinity;
for (var i = 0; i < values.length; ++i) {
min = Math.min(min, values[i]);
max = Math.max(max, values[i]);
}
this.range = max - min;
this.getMin = function() {
return min;
};
this.getMax = function() {
return max;
};
}
r = new RangeCalculator([1, 2, 3, 4, 5]);
console.log("min: " + r.getMin() + ", max: " + r.getMax());
<p>Check the console</p>
In that code, min
and max
aren't accessible from anywhere except functions defined within the constructor. This allows making private member variables in JavaScript.
But what if I just wanted to use the min and max variables to help me count the range, but didn't need them afterward. What if I remove the getMin and getMax functions. Will all the var
-declared variables used in the constructor still exist and take space in memory, including the i
from the for loop?
What if I wrap that code in a self-executing anonymous function?
function RangeCalculator(values) {
(function (t) {
var min = Infinity;
var max = -Infinity;
for (var i = 0; i < values.length; ++i) {
min = Math.min(min, values[i]);
max = Math.max(max, values[i]);
}
t.range = max - min;
})(this);
}
var r = new RangeCalculator([1, 2, 3, 4, 5]);
alert(r.range);