Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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 µÄ 10 ¼þÈÃÈ˷ѽâµÄÊÂÇé

JavaScript ¿ÉËãÊÇÊÀ½çÉÏ×îÁ÷Ðеıà³ÌÓïÑÔ£¬ËüÔø±» Web ¿ª·¢Éè¼ÆʦÌùÉÏجÃεıêÇ©£¬ËäÈ»ÕæÕýµÄجÃÎÆäʵÊÇ DOM API£¬Õâ¸ö±»´óÁ¿µÄ¿ª·¢ÓëÉè¼ÆʦËæÊÖÄéÀ´ÔöÇ¿ËûÃÇµÄ Web Ç°¶ËµÄ½Å±¾ÓïÑÔ£¬Èç½ñÔ½À´Ô½±»ÖØÊÓ£¬ËäÔòÈç´Ë£¬JavaScript ÈÔȻӵÓкܶàÈÃÈ˷ѽâµÄ¶«Î÷¡£
1. ËüÒÔ Java ÃüÃû£¬µ«²¢²»ÊÇ Java
Ëü×î³õ½Ð Mocha£¬ ½ÓןÄÃûÎ ......

JAVASCRIPT¶¯Ì¬µÄΪԪËØÌí¼Óʼþ

ÏÖÔڸоõjsºÜºÃºÜÇ¿´ó£¬Ëæ×ÅÉîÈëµÄѧϰ£¬Äã¾Í»á²»»á¾Ü¾øÔÚ¿Í»§¶ËʹÓÃjs¡£Ö®Ç°Ò²ÔÚÍøÉÏÕÒÁ˵ã×ÊÁÏ£¬Ò»Æð¿´¿´¡£
 
value="ÎÒÊÇ button" /> 
¶¯Ì¬Ìí¼Óonclickʼþ£º 
<input type="button" value="ÎÒÊÇ button" id="bu"> 
<script type="text/javascript"> 
var bObj=document. ......

³£ÓõÄJavaScriptÑéÖ¤ÕýÔò±í´ïʽ

±¾ÎĽ«½éÉܼ¸¸ö³£ÓõÄJavaScriptÑéÖ¤ÕýÔò±í´ïʽ£¬ÕâЩ×ÊÁÏÏ£Íû¶Ô´ó¼ÒÔÚʵ¼Ê±à³Ì¹ý³ÌÖÐÆðµ½°ïÖú×÷Óá£
Æ¥ÅäÖÐÎÄ×Ö·ûµÄÕýÔò±í´ïʽ£º [u4e00-u9fa5]
ÆÀ×¢£ºÆ¥ÅäÖÐÎÄ»¹ÕæÊǸöÍ·ÌÛµÄÊ£¬ÓÐÁËÕâ¸ö±í´ïʽ¾ÍºÃ°ìÁË
Æ¥ÅäË«×Ö½Ú×Ö·û(°üÀ¨ºº×ÖÔÚÄÚ)£º[^x00-xff]
ÆÀ×¢£º¿ÉÒÔÓÃÀ´¼ÆËã×Ö·û´®µÄ³¤¶È£¨Ò»¸öË«×Ö½Ú×Ö·û³¤¶È¼Æ2£¬ASCII×Ö·û ......

javascript withÓ÷¨(ת)

with
(object)
   statements
²ÎÊý
object
еÄĬÈ϶ÔÏó¡£
statements
Ò»¸ö»ò¶à¸öÓï¾ä£¬object ÊǸÃÓï¾äµÄĬÈ϶ÔÏó¡£
˵Ã÷
with
Óï¾äͨ³£ÓÃÀ´Ëõ¶ÌÌض¨ÇéÐÎϱØÐëдµÄ´úÂëÁ¿¡£ÔÚÏÂÃæµÄÀý×ÓÖУ¬Çë×¢Òâ Math µÄÖظ´Ê¹Óãº
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10)
y = Math.tan(14 * ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ