Local variables in functions seem to persist after the function is called. Is this always the case? How does this work?
// example 1
function Obj(str) {
this.show = function () { return str; };
}
var o1= new Obj("jeff");
var o2 = new Obj("eric");
o1.show(); // => "jeff" (unexpected?)
o2.show(); // => "eric"
The same thing happens here:
// example 2
function newObj(str) {
return { show: function () { return str; } };
}
var o3 = newObj("jeff");
var o4 = newObj("eric");
o3.show(); // => "jeff" (unexpected?)
o4.show(); // => "eric"
But what about in this case?
// example 3
function rand() {
var a = Math.random(0);
return a;
}
for (var i = 0; i < 1000000; i++) rand(); // are there a million random numbers stored somewhere?