Possible Duplicate:
Javascript closure inside loops - simple practical example
I seem to be struggling to understand the scope of variables in javascript.
I have the following code:
for (var i =2; i< 5;i++){
var k = (i+1).toString()
window['graph.point'+i].onMove = function(x,y){
return [min(x,window['graph.point'+k].coord[0]],f(min(x,window['graph.point'+k].coord[0]))]}
}
}
This code is supposed to put some constraints on the possible locations of some movable points I have on a graph. The problem seems to be that when the graph.point objects check to see if the constrain is satisfied, they use the last value of k, which is 6. How do I make sure these objects use the value of k that they are supposed to?