网上随便搜一个闭包的解释,动不动就长篇大论,各种专业名词,又扯上了内存泄露,各种用法……
我不想了解那么多没用的,能不能简单的告诉到底什么是闭包,我不关心闭包怎么就不能被销毁,怎么个指针指来指去的,看得我是越看头越晕!!
我得理解就是:
(函数里面有个局部变量,然后通过在把这个变量的值retrun给函数)
这个过程就是闭包特性了。这个用法是不是太常见了啊。。。怎么那么多大牛能写出来那么长的文章来
<code class="language-text">var counter = function() { var counter = 0 return function() { return counter++ } } var anotherCounter = counter() console.log(anotherCounter()) console.log(anotherCounter()) console.log(anotherCounter()) </code>
<code class="language-js"><span class="c1">// One of JavaScript's most powerful features is closures. If a function is</span> <span class="c1">// defined inside another function, the inner function has access to all the</span> <span class="c1">// outer function's variables, even after the outer function exits.</span> <span class="kd">function</span> <span class="nx">sayHelloInFiveSeconds</span><span class="p">(</span><span class="nx">name</span><span class="p">){</span> <span class="kd">var</span> <span class="nx">prompt</span> <span class="o">=</span> <span class="s2">"Hello, "</span> <span class="o">+</span> <span class="nx">name</span> <span class="o">+</span> <span class="s2">"!"</span><span class="p">;</span> <span class="c1">// Inner functions are put in the local scope by default, as if they were</span> <span class="c1">// declared with `var`.</span> <span class="kd">function</span> <span class="nx">inner</span><span class="p">(){</span> <span class="nx">alert</span><span class="p">(</span><span class="nx">prompt</span><span class="p">);</span> <span class="p">}</span> <span class="nx">setTimeout</span><span class="p">(</span><span class="nx">inner</span><span class="p">,</span> <span class="mi">5000</span><span class="p">);</span> <span class="c1">// setTimeout is asynchronous, so the sayHelloInFiveSeconds function will</span> <span class="c1">// exit immediately, and setTimeout will call inner afterwards. However,</span> <span class="c1">// because inner is "closed over" sayHelloInFiveSeconds, inner still has</span> <span class="c1">// access to the `prompt` variable when it is finally called.</span> <span class="p">}</span> <span class="nx">sayHelloInFiveSeconds</span><span class="p">(</span><span class="s2">"Adam"</span><span class="p">);</span> <span class="c1">// will open a popup with "Hello, Adam!" in 5s</span> </code>
<code class="language-js"><span class="kd">var</span> <span class="nx">globalVal</span><span class="o">=</span><span class="kc">null</span><span class="p">;</span> <span class="kd">var</span> <span class="nx">fn</span><span class="o">=</span><span class="kd">function</span><span class="p">(){</span> <span class="kd">var</span> <span class="nx">a</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">globalVal</span><span class="o">=</span><span class="kd">function</span><span class="p">(){</span> <span class="nx">a</span><span class="o">++</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">a</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="nx">fn</span><span class="p">();</span> <span class="nx">globalVal</span><span class="p">();</span><span class="c1">//2</span> <span class="nx">globalVal</span><span class="p">();</span><span class="c1">//3</span> </code>