1 Answer
1

You fell into the trap of declaring a closure inside a loop. The inner function closes ever the variable myDate and not over its value. This is particularly unexpected since variables are supposed to have block scope and not persist over multiple iterations (not the case in Javascript - everything is function scope).

You can work around this by having a closure-maker function. This way you can create a new instance of the variable to close over at will.

WRONG

var i;
for(i=0; i<xs.length; i++){
something = function(){
f(i);
};
}
//all closures share the i variable and will have it
//be i=xs.length in the end. We don't want that.