JavaScript Broken Closures and Wrapper Function from John Resig #62 and #63
<p>The first example below is #62 from John Resign`s Learning Advanced JavaScript <a href="http://ejohn.org/apps/learn/#62" rel="nofollow">http://ejohn.org/apps/learn/#62</a>. It is called Fix the Broken Closures.  Example 1 fails 4 times. Example 2, which is only different because it has a wrapper function, passes 4 times. It is example #63 from the same tutorial</p>

<p>Can someone please explain</p>

<p>1) why <code>i == count++</code> in example 1 fails. </p>

<p>2) why <code>i == count++</code> passes with the help of the wrapper function. How does the wrapper function change things to make it work?</p>

<p>Thanks in advance. </p>

<p><strong>Example 1</strong></p>

<pre><code>var count = 0; 
for ( var i = 0; i &lt; 4; i++ ) { 
  setTimeout(function(){ 
    assert( i == count++, "Check the value of i." ); 
  }, i * 200); 
}
</code></pre>

<p><strong>Example 2</strong></p>

<pre><code>var count = 0; 
for ( var i = 0; i &lt; 4; i++ ) (function(i){ 
  setTimeout(function(){ 
    assert( i == count++, "Check the value of i." ); 
  }, i * 200); 
})(i);
</code></pre>
