Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

Àí½âJavascript±Õ°ü(closure)

Ò»¡¢Ê²Ã´ÊDZհü£¿
“¹Ù·½”µÄ½âÊÍÊÇ£ºËùν“±Õ°ü”£¬Ö¸µÄÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÏàÐźÜÉÙÓÐÈËÄÜÖ±½Ó¿´¶®Õâ¾ä»°£¬ÒòΪËûÃèÊöµÄ̫ѧÊõ¡£ÎÒÏëÓÃÈçºÎÔÚJavascriptÖд´½¨Ò»¸ö±Õ°üÀ´¸æËßÄãʲôÊDZհü£¬ÒòΪÌø¹ý±Õ°üµÄ´´½¨¹ý³ÌÖ±½ÓÀí½â±Õ°üµÄ¶¨ÒåÊǷdz£À§Äѵġ£¿´ÏÂÃæÕâ¶Î´úÂ룺 
function a(){
 var i=0;
 function b(){
 alert(++i);
 }
 return b;
}
var c = a();
c();
Õâ¶Î´úÂëÓÐÁ½¸öÌص㣺
1¡¢º¯ÊýbǶÌ×ÔÚº¯ÊýaÄÚ²¿£»
2¡¢º¯Êýa·µ»Øº¯Êýb¡£
ÕâÑùÔÚÖ´ÐÐÍêvar c=a()ºó£¬±äÁ¿cʵ¼ÊÉÏÊÇÖ¸ÏòÁ˺¯Êýb£¬ÔÙÖ´ÐÐc()ºó¾Í»áµ¯³öÒ»¸ö´°¿ÚÏÔʾiµÄÖµ(µÚÒ»´ÎΪ1)¡£Õâ¶Î´úÂëÆäʵ¾Í´´½¨ÁËÒ»¸ö±Õ°ü£¬ÎªÊ²Ã´£¿ÒòΪº¯ÊýaÍâµÄ±äÁ¿cÒýÓÃÁ˺¯ÊýaÄڵĺ¯Êýb£¬¾ÍÊÇ˵£º
µ±º¯ÊýaµÄÄÚ²¿º¯Êýb±»º¯ÊýaÍâµÄÒ»¸ö±äÁ¿ÒýÓõÄʱºò£¬¾Í´´½¨ÁËÒ»¸ö±Õ°ü¡£
ÎÒ²ÂÏëÄãÒ»¶¨»¹ÊDz»Àí½â±Õ°ü£¬ÒòΪÄã²»ÖªµÀ±Õ°üÓÐʲô×÷Óã¬ÏÂÃæÈÃÎÒÃǼÌÐø̽Ë÷¡£
¶þ¡¢±Õ°üÓÐʲô×÷Óã¿
¼ò¶øÑÔÖ®£¬±Õ°üµÄ×÷ÓþÍÊÇÔÚaÖ´ÐÐÍê²¢·µ»Øºó£¬±Õ°üʹµÃJavascriptµÄÀ¬»ø»ØÊÕ»úÖÆGC²»»áÊÕ»ØaËùÕ¼ÓõÄ×ÊÔ´£¬ÒòΪaµÄÄÚ²¿º¯ÊýbµÄÖ´ÐÐÐèÒªÒÀÀµaÖеıäÁ¿¡£ÕâÊǶԱհü×÷Óõķdz£Ö±°×µÄÃèÊö£¬²»×¨ÒµÒ²²»ÑϽ÷£¬µ«´ó¸ÅÒâ˼¾ÍÊÇÕâÑù£¬Àí½â±Õ°üÐèҪѭÐò½¥½øµÄ¹ý³Ì¡£
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÓÉÓÚ±Õ°üµÄ´æÔÚʹµÃº¯Êýa·µ»Øºó£¬aÖеÄiʼÖÕ´æÔÚ£¬ÕâÑùÿ´ÎÖ´ÐÐc()£¬i¶¼ÊÇ×Ô¼Ó1ºóalert³öiµÄÖµ¡£
ÄǠôÎÒÃÇÀ´ÏëÏóÁíÒ»ÖÖÇé¿ö£¬Èç¹ûa·µ»ØµÄ²»ÊǺ¯Êýb£¬Çé¿ö¾ÍÍêÈ«²»Í¬ÁË¡£ÒòΪaÖ´ÐÐÍêºó£¬bûÓб»·µ»Ø¸øaµÄÍâ½ç£¬Ö»ÊDZ»aËùÒýÓ㬶ø´ËʱaÒ²Ö»»á±»bÒý Óã¬Òò´Ëº¯ÊýaºÍb»¥ÏàÒýÓõ«ÓÖ²»±»Íâ½ç´òÈÅ(±»Íâ½çÒýÓÃ)£¬º¯ÊýaºÍb¾Í»á±»GC»ØÊÕ¡£(¹ØÓÚJavascriptµÄÀ¬»ø»ØÊÕ»úÖƽ«ÔÚºóÃæÏêϸ½éÉÜ)
Èý¡¢±Õ°üÄÚµÄ΢¹ÛÊÀ½ç
Èç ¹ûÒª¸ü¼ÓÉîÈëµÄÁ˽â±Õ°üÒÔ¼°º¯ÊýaºÍǶÌ׺¯ÊýbµÄ¹Øϵ£¬ÎÒÃÇÐèÒªÒýÈëÁíÍ⼸¸ö¸ÅÄº¯ÊýµÄÖ´Ðл·¾³(excution context)¡¢»î¶¯¶ÔÏó(call object)¡¢×÷ÓÃÓò(scope)¡¢×÷ÓÃÓòÁ´(scope chain)¡£ÒÔº¯Êýa´Ó¶¨Òåµ½Ö´ÐеĹý³ÌΪÀý²ûÊöÕ⼸¸ö¸ÅÄî¡£
1¡¢µ±¶¨Ò庯ÊýaµÄʱºò£¬js½âÊÍÆ÷»á½«º¯ÊýaµÄ×÷ÓÃÓòÁ´(scope chain)ÉèÖÃΪ¶¨ÒåaʱaËùÔڵē»·¾³”£¬Èç¹ûaÊÇÒ»¸öÈ«¾Öº¯Êý£¬Ôòscope chainÖÐÖ»ÓÐwindow¶ÔÏó¡£
2¡¢


Ïà¹ØÎĵµ£º

µ¥ÒýºÅ,Ë«ÒýºÅ,javascript,HTML,תÒå×Ö·û

ÔÚÒ»¸öÍøÒ³Öеİ´Å¥£¬Ð´onclickʼþµÄ´¦Àí´úÂ룬²»Ð¡ÐÄд³ÉÈçÏ£º
<input value="Test" type="button" onclick="alert(""OK"");" />
IEÌáʾ³ö´íºó£¬ÔÙÂþ²»¾­ÐĵظÄΪ£º
<input value="Test" type="button" onclick="alert(\"OK\");" />
½á¹û»¹Êdzö´í¡£
Õâʱ£¬ÎÒ¾ÍÏ벻ͨÁË£¬ËäÈ»ÎÒÖªµÀ×îÖ±½ÓµÄ½â¾ö·½·¨ ......

ÓÃJavascript¶¯Ì¬Ìí¼Óɾ³ýHTMLÔªËØʵÀý

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ÓÃjavascript¶¯Ì¬Ìí¼Óɾ³ýhtmlÔªËØ</title>
<script type="text/javascript"><!-- 
function $(nodeId) { 
re ......

ÉîÈëÀí½â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»ÊÇ·ûºÏ´óСҪÇó£¬Æäʵ½Å±¾¿ÉÒԺܷ½±ãµÄ°ïÖúÎÒÃÇʵÏÖ£¬´úÂëÈçÏÂ
<html>
<head>
<title>JavaScript_»ñÈ¡Îļþ´óС_°¢·½Ë÷</title>
</head>
<body>
<input type="file" name="file1" id="file1" size="40" onchan ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ