Àí½â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¡¢
Ïà¹ØÎĵµ£º
ä¯ÀÀÆ÷£ºIE 8¡¢FF 3.6¡¢Chrome 4.0¡¢Safari 4.0¡¢Opera 10.1
½öÓÐIEä¯ÀÀÆ÷Ö§³ÖHTMLElement.onresize£¨±ÈÈçbody.onresize£©
ÆäËüä¯ÀÀÆ÷Ö»Ö§³Öwindow.onresize
ÏÈ˵IEµÄHTMLElement.onresize
ʹÓÃÇ°ÇëÈ·¶¨ÄãµÄÐÄÔ༰ѪѹÕý³££¬Èç¹ûÄ㶨ÒåÁË
body.onresize = function(){……}»òÕßhtml.onresize = obj.fun;
......
<!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Á¬½ÓaccessÊý¾Ý¿âµÄ·½·¨¡£Õâ¶Î´úÂëÔÚ±¾µØ¿ÉÒÔÕý³£ÔËÐУ¬ÔÚÏßÔòÐèÒª×ã¹»µÄȨÏÞ¡£ÓÉÓÚ²ÉÓÃAccessÊý¾Ý¿â£¬ËùÒÔ²¢²»Ê¹Óñê×¼µÄSQLÓ﷨ʹÓÃʱ¡£ÐèҪעÒâ¡£
Õ⼸Ìì·ÑÁËͦ´óÁ¦ÆøÔÚÍøÉϲéÕÒjavascriptÁ¬½ÓaccessÊý¾Ý¿âµÄ·½·¨£¬¾¹ýÕûÀí×ܽᣬÖÕÓÚд³öÒ»¶ÎÄܹ»ÓÐЧִÐеĴúÂë¡£
µ«ÊÇÕâ¶Î´úÂëÔÚ±¾µØ¿ÉÒÔÕý³£ÔËÐУ ......
±¾ÎļòÒª½éÉÜJavaScriptÖÐconfirm,alert,promptµÄÓ÷¨,Ï£Íû´ó¼Òѧϰ¹ý³ÌÖÐÄܵõ½Ò»Ð©Æô·¢¡£
window.confirm ²ÎÊý¾ÍÖ»ÓÐÒ»¸ö¡£ÏÔʾÌáʾ¿òµÄÐÅÏ¢¡£°´È·¶¨,·µ»Øtrue£»°´È¡Ïû·µ»Øfalse¡£
< SCRIPT> var bln = window.confirm("È·¶¨Âð?"); alert(bln) < /SCRIPT> window.alert²ÎÊý£¬Ö»ÓÐÒ»¸ö£¬ÏÔʾ ......
with
(object)
statements
²ÎÊý
object
еÄĬÈ϶ÔÏó¡£
statements
Ò»¸ö»ò¶à¸öÓï¾ä£¬object ÊǸÃÓï¾äµÄĬÈ϶ÔÏó¡£
˵Ã÷
with
Óï¾äͨ³£ÓÃÀ´Ëõ¶ÌÌض¨ÇéÐÎϱØÐëдµÄ´úÂëÁ¿¡£ÔÚÏÂÃæµÄÀý×ÓÖУ¬Çë×¢Òâ Math µÄÖظ´Ê¹Óãº
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10)
y = Math.tan(14 * ......