JavaScript 闭包环境非常奇特 - 相当于类与实例的关系?!
太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es)
本文遵循“署名-非商业用途-保持一致”创作公用协议
又一个疑问贴!
只是我相信。问题并非难在怎样解决。终于就是个能解决与不能解决,这非常easy!
但当你无法决断能否否解决,与怎样解决的时侯。这才是最让人纠结的事情。谁说不是呢?!
这篇转载,看了三遍了。好像没记住太多,只是有些明确了,闭包的含义。这当然得包含它的习性。要不怎么能算得上了解呢。
以下是一个更有意思的演示样例 — makeAdder
函数:
1
2
3
4
5
6
7
8
9
10
11
|
<code
class
=
" language-js"
style=
"margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-weight: inherit; font-family: Consolas, Monaco, 'Andale Mono', monospace; color: inherit; text-shadow: none; direction: ltr; word-spacing: normal; word-break: normal; tab-size: 4;"
><span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
function
</span> <span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>makeAdder<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span>x<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span> <span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>{</span>
<span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
return
</span> <span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
function
</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span>y<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span> <span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>{</span>
<span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
return
</span> x <span
class
=
"token operator"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(166, 127, 89); background: rgba(255, 255, 255, 0.498039);"
>+</span> y<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span>
<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>}</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span>
<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>}</span>
<span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
var
</span> add5 <span
class
=
"token operator"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(166, 127, 89); background: rgba(255, 255, 255, 0.498039);"
>=</span> <span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>makeAdder<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token number"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 0, 85);"
>5</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span>
<span
class
=
"token keyword"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(0, 119, 170);"
>
var
</span> add10 <span
class
=
"token operator"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(166, 127, 89); background: rgba(255, 255, 255, 0.498039);"
>=</span> <span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>makeAdder<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token number"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 0, 85);"
>10</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span>
<span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>print<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>add5<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token number"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 0, 85);"
>2</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span> <span
class
=
"token comment"
spellcheck=
"true"
style=
"margin: 0px; padding: 0px; border: 0px; display: inherit; color: rgb(112, 128, 144);"
>
// 7
</span><span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>print<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token function"
style=
"margin: 0px; padding: 0px; border: 0px;"
>add10<span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>(</span></span><span
class
=
"token number"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 0, 85);"
>2</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>)</span><span
class
=
"token punctuation"
style=
"margin: 0px; padding: 0px; border: 0px; color: rgb(153, 153, 153);"
>;</span><span
class
=
"token comment"
spellcheck=
"true"
style=
"margin: 0px; padding: 0px; border: 0px; display: inherit; color: rgb(112, 128, 144);"
>
// 12</span></code><div class="line-number" data-start="1" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 0px; background: transparent;"></div><div class="line-number" data-start="2" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 19px; background: transparent;"></div><div class="line-number" data-start="3" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 38px; background: transparent;"></div><div class="line-number" data-start="4" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 57px; background: transparent;"></div><div class="line-number" data-start="5" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 76px; background: transparent;"></div><div class="line-number" data-start="6" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 95px; background: transparent;"></div><div class="line-number" data-start="7" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 114px; background: transparent;"></div><div class="line-number" data-start="8" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 133px; background: transparent;"></div><div class="line-number" data-start="9" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 152px; background: transparent;"></div><div class="line-number" data-start="10" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 171px; background: transparent;"></div><div class="line-number" data-start="11" style="margin: 1em 0px 0px; padding: 0px; border: 0px; position: absolute; left: 0px; right: 0px; pointer-events: none; line-height: inherit; top: 190px; background: transparent;"></div>
|
在这个演示样例中,我们定义了 makeAdder(x)
函数:带有一个參数 x
并返回一个新的函数。返回的函数带有一个參数 y
。并返回 x
和 y
的和。
那么再或者,这个闭包能否够当成是一个类呢?而事实上例对象确是个看不到摸不着。确能随其成员的销毁而销毁的后台英雄呢。这就是闭包环境吧,也就是闭包相当于类,闭包环境相当于类的实例,拥有着一份独一无二,不随类存在的成员变量环境。
并且。闭包能够定义的同一时候运行,那必须在后面加上 (),这就是函数调用的那两个左右括号。
但到眼下尚未找到我须要的部分,难道还有其他的运行方式?
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5097733.html,如需转载请自行联系原作者