ÉîÈëÀí½âJavascript±Õ°ü(closure)
×î½üÔÚÍøÉϲéÔÄÁ˲»ÉÙJavascript±Õ°ü(closure)Ïà¹ØµÄ×ÊÁÏ£¬Ð´µÄ´ó¶àÊǷdz£µÄѧÊõºÍרҵ¡£¶ÔÓÚ³õѧÕßÀ´Ëµ±ð˵Àí½â±Õ°üÁË£¬¾ÍÁ¬ÎÄ×ÖÐðÊö¶¼ºÜÄÑ¿´¶®¡£×«Ð´´ËÎĵÄÄ¿µÄ¾ÍÊÇÓÃ×îͨË×µÄÎÄ×Ö½Ò¿ªJavascript±Õ°üµÄÕæʵÃæÄ¿¡£
Ò»¡¢Ê²Ã´Ê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µÄ×÷
Ïà¹ØÎĵµ£º
ÀûÓÃjsÀ´¶¯Ì¬´´½¨±í¸ñÓÐÁ½ÖÖ¸ñʽ£¬appendChild()ºÍinsertRow¡¢insertCell()¡£Á½ÖÖ·½Ê½Æäʵ²î²»¶à£¬µ«µÚÒ»ÖÖÓпÉÄÜÔÚIEÉÏÓÐÎÊÌ⣬ËùÒÔÍƼö´ó¼ÒʹÓõڶþÖÖÁË£¬Ö±½Ó˵°É¡£
1¡¢inserRow()ºÍinsertCell()º¯Êý
insertRow()º¯Êý¿ÉÒÔ´ø²ÎÊý£¬ÐÎʽÈçÏ£º
insertRow(index)£ºindex´Ó0¿ªÊ¼
¡¡¡¡Õâ¸öº¯Êý½«ÐÂÐÐÌí¼Óµ½indexµÄÄÇÒ»ÐÐÇ ......
Optimizing JavaScript code
Authors: Gregory Baker, Software Engineer on GMail & Erik Arvidsson, Software Engineer on Google Chrome
Recommended experience: Working knowledge of JavaScript
Client-side
scripting can make your application dynamic and active, but the
browser's interpretation o ......
ÔÚJavaScriptÖÐûÓжàάÊý×éµÄ¸ÅÄî
µ«ÊÇ¿ÉÒÔÄ£ÄâʵÏÖ
1ά
//1 ²»ÄÜÈ·¶¨Êý×élengthµÄÇé¿öÏ£¬ÏÈÉùÃ÷Ò»¸öÊý×éÃû£¬È»ºóÔÙÖð¸ö¸³Öµ¡£
var tArray=new Array();
tArray[0]='A';
tArray[1]='2';
//2 ÖªµÀÈ·ÇеÄlength£¬´´½¨Êý×é
var tArray=new Array(10);
//3 ´´½¨Êý×éµÄͬʱ²¢¸³Öµ
var tArray=new Array('A','2','3', ......
ÔÚjavascriptÖжÔÏóͨ³£°üÀ¨Á½ÖÖÀàÐÍ£ºÄÚÖöÔÏóºÍä¯ÀÀÆ÷¶ÔÏ󣬴ËÍ⣬Óû§»¹¿ÉÒÔ×Ô¶¨Òå¶ÔÏó¡£
¶ÔÏó°üº¬Á½¸öÒªËØ£º1.ÓÃÀ´ÃèÊö¶ÔÏóÌØÐÔµÄÒ»×éÊý¾Ý£¬Ò²¾ÍÊÇÈô¸É±äÁ¿£¬Í¨³£³ÆΪÊôÐÔ¡£2.ÓÃÀ´²Ù×÷¶ÔÏóÌØÐÔµÄÈô¸É¶¯×÷£¬Ò²¾ÍÊÇÈô¸Éº¯Êý£¬Í¨³£³ÆΪ·½·¨¡£
ä¯ÀÀÆ÷¶ÔÏó
¶ÔÏó
º¬Òå
anchor
µ±Ç°ÎĵµÖÐÉèÖÃÁËnameÊôÐԵij¬Á´½Ó
appl ......