Javascript variables inherited by functions
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/5555464/javascript-closure-of-loop">Javascript: closure of loop?</a>  </p>
</blockquote>



<p>Pseudo javascript:</p>

<pre><code>for (i, i&lt;10, i++) {
  new element.addEvent('click', function(){ alert(i) }).inject(dom)
}
</code></pre>

<p>When the loop finishes all the onclick events will trigger alerts with the 'final' value of i. What is the 'correct' way of having them alert the value of i as it was when the onclick event was added?</p>

<p>When I say correct I mean that I'm aware there are several ways of achiving this behavior, but I want to know the standard (ie expected by people that may encounter the code) way. Thanks.</p>
