<?xml version="1.0" encoding="UTF-8"?> 
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        >
<channel>
  <title>asgaard</title>
  <description></description>
  <link>https://blog.asgaard.co.uk/2013/02/08/closures-for-loops-and-scoping-in-javascript</link>
  <lastBuildDate>Tue, 02 Jun 26 08:26:50 +0000</lastBuildDate>
  <language>en</language>
  <count>1</count>
  <offset>0</offset>
      <item>
    <title>Closures, for loops and scoping in JavaScript</title>
    <link>https://blog.asgaard.co.uk/2013/02/08/closures-for-loops-and-scoping-in-javascript</link>
    <pubDate>Fri, 08 Feb 13 21:33:33 +0000</pubDate>
    <guid>https://blog.asgaard.co.uk/2013/02/08/closures-for-loops-and-scoping-in-javascript</guid>
    <description><![CDATA[
<p>
<pre>var funcs = [];
for (var i = 0; i &lt; 10; i++) {
    funcs.push( function() { console.log(i) } );
}</pre>
<p>
What does <code>funcs[0]()</code> output?
<p>
If you said 10, you were right.
<p>
The key here is that although <code>i</code> is accessible from the scope of the closure, it can be modified by other things. The closure doesn&#039;t copy the values it can access when it&#039;s created. 
<p>
In some cases, this kind of thing does work as expected. That&#039;s because in those cases, you are accessing a value that doesn&#039;t change. Even though it looks like it might have. If you combine with a forEach function, you actually get your own value that won&#039;t change.
<p>
You can also do this by creating a copy inside a function expression:
<p>
<pre>var funcs = [];
for (var i = 0; i &lt; 10; i++) {
    (function(i) {
        funcs.push( function() { console.log(i) } );
    }(i));
}</pre>
<p>
<code>funcs[0]()</code> =&gt; 0.
<p>
Note that the <code>i</code> accessible within the closure is a different <code>i</code> from the for loop&#039;s.
<p>
If you&#039;re using any helper libraries (or Node) with your [...]]]></description>
    <content:encoded><![CDATA[
<p>
<pre>var funcs = [];
for (var i = 0; i &lt; 10; i++) {
    funcs.push( function() { console.log(i) } );
}</pre>
<p>
What does <code>funcs[0]()</code> output?
<p>
If you said 10, you were right.
<p>
The key here is that although <code>i</code> is accessible from the scope of the closure, it can be modified by other things. The closure doesn&#039;t copy the values it can access when it&#039;s created. 
<p>
In some cases, this kind of thing does work as expected. That&#039;s because in those cases, you are accessing a value that doesn&#039;t change. Even though it looks like it might have. If you combine with a forEach function, you actually get your own value that won&#039;t change.
<p>
You can also do this by creating a copy inside a function expression:
<p>
<pre>var funcs = [];
for (var i = 0; i &lt; 10; i++) {
    (function(i) {
        funcs.push( function() { console.log(i) } );
    }(i));
}</pre>
<p>
<code>funcs[0]()</code> =&gt; 0.
<p>
Note that the <code>i</code> accessible within the closure is a different <code>i</code> from the for loop&#039;s.
<p>
If you&#039;re using any helper libraries (or Node) with your for loops you don&#039;t need to run into this too often. The above is equivalent to:
<p>
<pre>var funcs = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(function(i) {
    funcs.push( function() { console.log(i) } );
});</pre>
<p>
]]></content:encoded>
  </item>
  </channel>
</rss>