Issue with scope of variables in javascript
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example">Javascript closure inside loops - simple practical example</a>  </p>
</blockquote>



<p>I seem to be struggling to understand the scope of variables in javascript.</p>

<p>I have the following code:</p>

<pre><code>for (var i =2; i&lt; 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]))]}
    }
}
</code></pre>

<p>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?</p>
