最近看了一些 JavaScript 的内存泄露问题,看似没问题的代码原来存在内存泄露,而且部分还不知道怎么回事,比如:
<span class="kd">function</span> <span class="p">(</span><span class="nx">element</span><span class="p">,</span><span class="nx">a</span><span class="p">,</span><span class="nx">b</span><span class="p">){</span> <span class="nx">element</span><span class="p">.</span><span class="nx">onclick</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span> <span class="c1">//TODO a b here</span> <span class="p">}</span> <span class="p">}</span>
<code class="language-js"><span class="kd">var</span> <span class="nx">test_obj</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">closure_fn</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">val</span> <span class="o">=</span> <span class="nx">setTimeout</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="s1">'Rambo!'</span><span class="p">);</span>
<span class="nx">that</span><span class="p">.</span><span class="nx">closure_fn</span><span class="p">();</span>
<span class="p">},</span> <span class="mi">1000</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="nx">test_obj</span><span class="p">.</span><span class="nx">closure_fn</span><span class="p">();</span>
<span class="nx">test_obj</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
<span class="c1">// 尝试之后,你会发现这他妈才是可怕的。</span>
<span class="kd">var</span> <span class="nx">test_obj_2</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">closure_fn</span><span class="o">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="nx">test_obj_2</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">val</span> <span class="o">=</span> <span class="nx">setTimeout</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="s1">'Rambo!'</span><span class="p">);</span>
<span class="nx">that</span> <span class="o">?</span> <span class="nx">that</span><span class="p">.</span><span class="nx">closure_fn</span><span class="p">()</span> <span class="o">:</span> <span class="nx">clearTimeout</span><span class="p">(</span><span class="nx">val</span><span class="p">);</span>
<span class="p">},</span> <span class="mi">1000</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">};</span>
</code>
我记得 @贺师俊 老师在ITEYE上有过一段关于这个的讨论。