Home > Article > Backend Development > js关于for循环中的闭包问题?
for(var i=0,arr=[];i arr.push(function(){alert(i)});
}
arr[0](); // ?? 结果不是0
arr[1](); // ?? 全是4
改装后
for(var i=0,arr=[];i arr.push(
(function(i){
return function(){
alert(i);
}
})(i)
);
}
使用闭包可以解决了,为什么第一次代码中的i读取的一直是I变量的最后的结果呢?
那个大神能给分析一下第一段代码的执行的具体步骤呢?
图例:绿框为函数执行的栈内存,粉色为堆内存,0XAA为内存地址(存储着函数的字符串,并不会执行,当函数被调用时才会拿出来执行),沿着主流程从上到下看来看在修改之前的原始版分中,arr中每一项执行时,都会去上级作用域寻找i,而i在for循环执行结束后就已经变成了4,所以arr中每一项执行的结果都是一样的。函数在预解释阶段,都被当成字符串存入堆内存,在真正执行时,才会被拿出来执行,数组中存储的,其实只是指向这个堆内存的指针,i并没有传进去,执行的时候i才被传进去。
<code class="language-js"><span class="kd">var</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[],</span> <span class="nx">i</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o"> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span> <span class="c1">// 3</span>
<span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]();</span> <span class="c1">// 3</span>
<span class="nx">i</span> <span class="o">=</span> <span class="mi">5</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]();</span> <span class="c1">// 5</span>
</span></code>
理解这个问题的关键是execution context,简单说就是当一个function被call的时候,它能“看到”哪些地方的变量名。为了简化问题你暂时可以考虑execution context只由以下两种组成:1)Global scope也就是全局的变量,2)每个function被call的时候在这个function里面定义的变量。<code class="language-js"><span class="kd">var</span> <span class="nx">globalVar</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">outerVar</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">innerVar</span><span class="p">;</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">globalVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">outerVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">innerVar</span><span class="p">);</span>
<span class="kd">function</span> <span class="nx">outer</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">function</span> <span class="nx">inner</span><span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">innerVar</span> <span class="o">=</span> <span class="mi">2</span><span class="p">;</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">globalVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">outerVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">innerVar</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">outerVar</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">globalVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">outerVar</span><span class="p">);</span>
<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">innerVar</span><span class="p">);</span>
<span class="nx">inner</span><span class="p">();</span>
<span class="p">}</span>
<span class="nx">outer</span><span class="p">();</span>
</code>
关于本题:<code class="language-text">for(var i=0,arr=[];i</code>
<code class="language-js"><span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">arr</span> <span class="o">=</span> <span class="p">[];</span> <span class="nx">i</span> <span class="o"> <span class="mi">3</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="nx">alert</span><span class="p">(</span><span class="nx">j</span><span class="p">)</span>
<span class="p">});</span>
<span class="p">}</span>
</span></code>
因为js没有块级作用域<code class="language-js"><span class="kd">var</span> <span class="nx">arr</span><span class="o">=</span><span class="p">[];</span>
<span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);});</span>
<span class="nx">i</span><span class="o">++</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);});</span>
<span class="nx">i</span><span class="o">++</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);});</span>
<span class="nx">i</span><span class="o">++</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="kd">function</span><span class="p">(){</span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">i</span><span class="p">);});</span>
<span class="nx">i</span><span class="o">++</span><span class="p">;</span>
<span class="nx">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]();</span>
<span class="nx">arr</span><span class="p">[</span><span class="mi">3</span><span class="p">]();</span>
</code>