ÉîÈëÀí½âJavaScript±Õ°ü(closure)
ת×Ô£ºhttp://www.felixwoo.com/?s=%E9%97%AD%E5%8C%85
ÉîÈëÀí½âJavaScript±Õ°ü(closure)
·¢±íÓÚ: 14:48 | ·ÖÀà: ¿ª·¢¼¼Êõ | ÆÀÂÛ: 25 | ÈËÆø: 6,114 |
×î½üÔÚÍøÉϲéÔÄÁ˲»ÉÙJavascript±Õ°ü(closure)Ïà¹ØµÄ×ÊÁÏ£¬Ð´µÄ´ó¶àÊǷdz£µÄѧÊõºÍרҵ¡£¶ÔÓÚ³õѧÕßÀ´Ëµ±ð˵Àí½â±Õ°üÁË£¬¾ÍÁ¬ÎÄ×ÖÐðÊö¶¼ºÜÄÑ¿´¶®¡£×«Ð´´ËÎĵÄÄ¿µÄ¾ÍÊÇÓÃ×îͨË×µÄÎÄ×Ö½Ò¿ªJavascript±Õ°üµÄÕæʵÃæÄ¿¡£
Ò»¡¢Ê²Ã´ÊDZհü£¿
“¹Ù·½”µÄ½âÊÍÊÇ£º±Õ°üÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÏàÐźÜÉÙÓÐÈËÄÜÖ±½Ó¿´¶®Õâ¾ä»°£¬ÒòΪËûÃèÊöµÄ̫ѧÊõ¡£ÆäʵÕâ¾ä»°Í¨Ë×µÄÀ´Ëµ¾ÍÊÇ£ºJavaScriptÖÐËùÓеÄfunction¶¼ÊÇÒ»¸ö±Õ°ü¡£²»¹ýÒ»°ãÀ´Ëµ£¬Ç¶Ì×µÄfunctionËù²úÉúµÄ±Õ°ü¸üΪǿ´ó£¬Ò²ÊǴ󲿷ÖʱºòÎÒÃÇËùνµÄ“±Õ°ü”¡£¿´ÏÂÃæÕâ¶Î´úÂ룺
function a() {
var i = 0;
function b() {
alert(++i);
}
return b;
}
var c = a();
c();
Õâ¶Î´úÂëÓÐÁ½¸öÌص㣺
º¯ÊýbǶÌ×ÔÚº¯ÊýaÄÚ²¿£»
º¯Êýa·µ»Øº¯Êýb¡£
ÒýÓùØϵÈçͼ£º
ÕâÑùÔÚÖ´ÐÐÍêvar c=a()ºó£¬±äÁ¿cʵ¼ÊÉÏÊÇÖ¸ÏòÁ˺¯Êýb£¬bÖÐÓõ½Á˱äÁ¿i£¬ÔÙÖ´ÐÐc()ºó¾Í»áµ¯³öÒ»¸ö´°¿ÚÏÔʾiµÄÖµ(µÚÒ»´ÎΪ1)¡£Õâ¶Î´úÂëÆäʵ¾Í´´½¨ÁËÒ»¸ö±Õ°ü£¬ÎªÊ²Ã´£¿ÒòΪº¯ÊýaÍâµÄ±äÁ¿cÒýÓÃÁ˺¯ÊýaÄڵĺ¯Êýb£¬¾ÍÊÇ˵£º
µ±º¯ÊýaµÄÄÚ²¿º¯Êýb±»º¯ÊýaÍâµÄÒ»¸ö±äÁ¿ÒýÓõÄʱºò£¬¾Í´´½¨ÁËÒ»¸öÎÒÃÇͨ³£ËùνµÄ“±Õ°ü”¡£
ÈÃÎÒÃÇ˵µÄ¸ü͸³¹Ò»Ð©¡£Ëùν“±Õ°ü”£¬¾ÍÊÇÔÚ¹¹Ô캯ÊýÌåÄÚ¶¨ÒåÁíÍâµÄº¯Êý×÷ΪĿ±ê¶ÔÏóµÄ·½·¨º¯Êý£¬¶øÕâ¸ö¶ÔÏóµÄ·½·¨º¯Êý·´¹ýÀ´ÒýÓÃÍâ²ãÍâ²ãº¯ÊýÌåÖеÄÁÙʱ±äÁ¿¡£ÕâʹµÃֻҪĿ±ê ¶ÔÏóÔÚÉú´æÆÚÄÚʼÖÕÄܱ£³ÖÆä·½·¨£¬¾ÍÄܼä½Ó±£³ÖÔ¹¹Ô캯ÊýÌ嵱ʱÓõ½µÄÁÙʱ±äÁ¿Öµ¡£¾¡¹Ü×ʼµÄ¹¹Ô캯Êýµ÷ÓÃÒѾ½áÊø£¬ÁÙʱ±äÁ¿µÄÃû³ÆÒ²¶¼ÏûʧÁË£¬µ«ÔÚÄ¿ ±ê¶ÔÏóµÄ·½·¨ÄÚȴʼÖÕÄÜÒýÓõ½¸Ã±äÁ¿µÄÖµ£¬¶øÇÒ¸ÃÖµÖ»ÄÜͨÕâÖÖ·½·¨À´·ÃÎÊ¡£¼´Ê¹Ôٴε÷ÓÃÏàͬµÄ¹¹Ô캯Êý£¬µ«Ö»»áÉú³ÉжÔÏóºÍ·½·¨£¬ÐµÄÁÙʱ±äÁ¿Ö»ÊǶÔӦРµÄÖµ£¬ºÍÉÏ´ÎÄǴε÷ÓõÄÊǸ÷×Ô¶ÀÁ¢µÄ¡£
ΪÁ˸üÉî¿ÌµÄÀí½â±Õ°ü£¬ÏÂÃæÈÃÎÒÃǼÌÐø̽Ë÷±Õ°üµÄ×÷ÓúÍЧ¹û¡£
¶þ¡¢±Õ°üÓÐʲô×÷ÓúÍЧ¹û£¿
¼ò¶øÑÔÖ®£¬±Õ°üµÄ×÷ÓþÍÊÇÔÚaÖ´ÐÐÍê²¢·µ»Øºó£¬±Õ°üʹµÃJavascriptµÄÀ¬»ø»ØÊÕ»úÖÆGC²»»áÊÕ»ØaËùÕ¼ÓõÄ×ÊÔ´£¬ÒòΪaµÄÄÚ²¿º¯ÊýbµÄÖ´ÐÐÐèÒªÒÀÀµaÖеıäÁ¿¡£ÕâÊǶԱհü×÷Óõķdz£Ö±°×µÄÃèÊö£¬²»×¨
Ïà¹ØÎĵµ£º
JavaScript ¿ÉËãÊÇÊÀ½çÉÏ×îÁ÷Ðеıà³ÌÓïÑÔ£¬ËüÔø±» Web ¿ª·¢Éè¼ÆʦÌùÉÏجÃεıêÇ©£¬ËäÈ»ÕæÕýµÄجÃÎÆäʵÊÇ DOM
API£¬Õâ¸ö±»´óÁ¿µÄ¿ª·¢ÓëÉè¼ÆʦËæÊÖÄéÀ´ÔöÇ¿ËûÃÇµÄ Web Ç°¶ËµÄ½Å±¾ÓïÑÔ£¬Èç½ñÔ½À´Ô½±»ÖØÊÓ£¬ËäÔòÈç´Ë£¬JavaScript
ÈÔȻӵÓкܶàÈÃÈ˷ѽâµÄ¶«Î÷¡£
1. ËüÒÔ Java ÃüÃû£¬µ«²¢²»ÊÇ Java
Ëü×î³õ½Ð Mocha£¬ ½Óן ......
ͻȻ¼äÏëÈ¥ÏÂÊ×fire in the holeµÄÁåÉù£¬ÓÚÊÇÓÃsosoÕÒÁËһϣ¬½øÈëÁËÊÖ»úÀÖÔ° £¨http://download.shouji.com.cn/do/wave?html=&inputname=wave&wname=Fire+in+the+hole+&thsubmit=%E6%90%9C%E7%B4%A2£©£¬Ñ¡Ôñfire in the hole£¬µ¯³ö¸ö´°¿Ú£¬ÌáʾÈÃÊäÈëÊÖ»úºÅÂë¡£ÔÎËÀ£¬²»¾ÍÏÂÊ׸èÂ»¹²»ÈÃÖ±½ÓÏ¡£ºÇºÇ£¬·¢ÏÖÍ ......
Ê×ÏÈÔÚbodyÖмÓÈëÒÔÏÂHTMLÄÚÈÝ£º
<div id="panel">
<div id="top">
<ul id="menu">
<li id="1">Home</li>
&nbs ......
//´´½¨Ò»¸öеÄÔªËؽڵ㣬ԪËØÃûʹÓÃsTagName¶¨Òå
oElementNode = document.createElementNode(sTagName);
//´´½¨Ò»¸öеĽڵ㣬½ÚµãÃûʹÓÃsTextValue¶¨Òå
oTextNode = document.createTextNode(sTextValue);
//ΪԪËظ³Ò»¸öеÄÊôÐÔ£¬ÊôÐÔÃûʹÓÃsName
oAttribute = document.createAttribute(sName);
//´´½¨Ò»¸öÐ嵀 ......
JavaScript ¿ÉËãÊÇÊÀ½çÉÏ×îÁ÷Ðеıà³ÌÓïÑÔ£¬ËüÔø±» Web ¿ª·¢Éè¼ÆʦÌùÉÏجÃεıêÇ©£¬ËäÈ»ÕæÕýµÄجÃÎÆäʵÊÇ DOM API£¬Õâ¸ö±»´óÁ¿µÄ¿ª·¢ÓëÉè¼ÆʦËæÊÖÄéÀ´ÔöÇ¿ËûÃÇµÄ Web Ç°¶ËµÄ½Å±¾ÓïÑÔ£¬Èç½ñÔ½À´Ô½±»ÖØÊÓ£¬ËäÔòÈç´Ë£¬JavaScript ÈÔȻӵÓкܶàÈÃÈ˷ѽâµÄ¶«Î÷¡£
1. ËüÒÔ Java ÃüÃû£¬µ«²¢²»ÊÇ Java
Ëü×î³õ½Ð Mocha£¬ ½ÓןÄÃûÎ ......