±Õ°ü£¨closure£©ÊÇJavascriptÓïÑÔµÄÒ»¸öÄѵ㣬ҲÊÇËüµÄÌØÉ«£¬ºÜ¶à¸ß¼¶Ó¦Óö¼ÒªÒÀ¿¿±Õ°üʵÏÖ¡£
ÏÂÃæ¾ÍÊÇÎÒµÄѧϰ±Ê¼Ç£¬¶ÔÓÚJavascript³õѧÕßÓ¦¸ÃÊǺÜÓÐÓõġ£
Ò»¡¢±äÁ¿µÄ×÷ÓÃÓò
ÒªÀí½â±Õ°ü£¬Ê×ÏȱØÐëÀí½âJavascriptÌØÊâµÄ±äÁ¿×÷ÓÃÓò¡£
±äÁ¿µÄ×÷ÓÃÓòÎ޷ǾÍÊÇÁ½ÖÖ£ºÈ«¾Ö±äÁ¿ºÍ¾Ö²¿±äÁ¿¡£
JavascriptÓïÑÔµÄÌØÊâÖ®´¦£¬¾ÍÔÚÓÚº¯ÊýÄÚ²¿¿ÉÒÔÖ±½Ó¶ÁÈ¡È«¾Ö±äÁ¿¡£
¡¡¡¡var n=999;
¡¡¡¡function f1(){
¡¡¡¡¡¡¡¡alert(n);
¡¡¡¡}
¡¡¡¡f1(); // 999
ÁíÒ»·½Ã棬ÔÚº¯ÊýÍⲿ×ÔÈ»ÎÞ·¨¶ÁÈ¡º¯ÊýÄڵľֲ¿±äÁ¿¡£
¡¡¡¡function f1(){
¡¡¡¡¡¡¡¡var n=999;
¡¡¡¡}
¡¡¡¡alert(n); // error
ÕâÀïÓÐÒ»¸öµØ·½ÐèҪעÒ⣬º¯ÊýÄÚ²¿ÉùÃ÷±äÁ¿µÄʱºò£¬Ò»¶¨ÒªÊ¹ÓÃvarÃüÁî¡£Èç¹û²»ÓõĻ°£¬Äãʵ¼ÊÉÏÉùÃ÷ÁËÒ»¸öÈ«¾Ö±äÁ¿£¡
¡¡¡¡function f1(){
¡¡¡¡¡¡¡¡n=999;
¡¡¡¡}
¡¡¡¡f1();
¡¡¡¡alert(n); // 999
¶þ¡¢ÈçºÎ´ÓÍⲿ¶ÁÈ¡¾Ö²¿±äÁ¿£¿
³öÓÚÖÖÖÖÔÒò£¬ÎÒÃÇÓÐʱºòÐèÒªµÃµ½º¯ÊýÄڵľֲ¿±äÁ¿¡£µ«ÊÇ£¬Ç°ÃæÒѾ˵¹ýÁË£¬Õý³£Çé¿öÏ£¬ÕâÊÇ°ì²»µ½µÄ£¬Ö»ÓÐͨ¹ý±äͨ·½·¨²ÅÄÜʵÏÖ¡£
ÄǾÍÊÇÔÚº¯ÊýµÄÄÚ²¿£¬ÔÙ¶¨ÒåÒ»¸öº¯Êý¡£
¡¡¡¡function f1(){
¡¡¡¡¡¡¡¡n=999;
¡¡¡¡¡¡¡¡function f2(){
¡¡¡¡¡¡¡¡¡¡¡¡alert(n); ......
"These memory leaks often
occur as a result of circular references between JavaScript objects and
objects within IE’s DOM (document object model)."
GPDE Team Blog
Ã÷ÏÔµÄDOM¶ÔÏóÓë JavaScript¶ÔÏóÑ»·ÒýÓúܺÃÅжϣ¬ÄѵÄÊÇÒþº¬µÄÑ»·ÒýÓÃÅжϣ¡
Òþº¬µÄÑ»·ÒýÓÃÐèҪͨ¹ý×÷ÓÃÓòÁ´½øÐзÖÎöÅжϣ¡
¿¼ÂÇÒÔÏ´úÂë(¸Ðл¿É°®µÄwinter ^_^)£º
var e=document.getElementBy??("XX");
e.onclick=function(){}
scopechain·ÖÎö£º
scopechain.e = document.getElementBy??("XX");
function f(){}
function f(){}.[[scope]] = scopechain
e.onclick = f
ºÜÇåÎú£¬¶Ô²»¶Ô£¿
"Javascript°ó¶¨Ê¼þʱ£¬Ö»ÒªDOMµÄʼþÀï·ÃÎʲ»ÁËDOM×Ô¼ºµÄÄǸö±äÁ¿¾ÍÐÐÁË"
winter
"IEÏÖÔÚµÄPatch¸ãµÃÓÐʱºòй¶ÓÐʱºò²»Ð¹Â¶"
winter
<button id="testx">No Memory Leak Event Bind</button>
<button id="testy">No Memory Leak Event Bind</button>
<script type="text/javascript">
var fooA = function(){
var f = function(){ale ......
ÉîÈëÀí½âJavascript±Õ°ü
×î½üÔÚÍøÉϲéÔÄÁ˲»ÉÙ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ÄÚ²¿£»
¡ ......
Õâ¸öÎÊÌâºÜ¼òµ¥£¬Ö÷ÒªÓÐÏÂÃ漸¸ö֪ʶµã£º
£¨1£© È¡µÃʱ¼ä£ºvar d=new Date();var time=d.toLocaleString()
£¨2£© ÏÔʾÔÚÍøÒ³ÉÏ£¬¼ÙÉèдÔÚÒ»¸ö<span>ÖУ¬ÇÒ¸Ã<span>µÄidΪshowTime:
document.getElementById("showTime").innerHTML=time;
£¨3£© ÿ¼ä¸ôÒ»ÃëÏÔʾһ´Î£ºsetInterval('showTime()',1000)//¸Ãº¯Êý±íÃ÷£¬Ã¿¸ôÒ»ÃëÖÓµ÷Óú¯ÊýshowTime()Ò»´Î¡£
¾ßÌå´úÂëÈçÏ£º
<html>
<head>
<title></title>
</head>
<script language="javascript">
function showTime(){
var d=new Date();
var time=d.toLocaleString()
document.getElementById("showTime").innerHTML=time;
}
</script>
<body onload="setInterval('showTime()',1000)">
<span id="showTime"></span>
</body>
</html> ......
ҪʵÏÖÕâ¸ö¹¦ÄܹؼüÊÇÒªÀí½â¿éµÄdisplayÊôÐÔ¡£Ò»¸ö¿éµÄdisplayÊôÐÔÉèΪnone£¬¾ÍÏ൱ÓÚÕâ¸ö¿é²»´æÔÚ¡£ËùÒÔ½«ÒªÏÔʾµÄ¶à¸ö¿éµÄdisplayÊôÐÔÉèΪnone,ÔÙ¸ù¾ÝÐèÒª½«ÒªÏÔʾµÄ¿éµÄdisplayÊôÐÔÉèΪblock¾Í¿ÉÒÔ×ö³ö±êǩҳЧ¹ûÁË¡£
£¨1£© ½¨±êÇ©ÌâÒÔ¼°¸÷±êÇ©ÌâËù¶ÔÓ¦µÄÏÔʾÄÚÈÝ£º
<span id="span1">±êÇ©Ò³Ò»</span><span id="span2">±êÇ©Ò³¶þ</span><span id="span3">±êÇ©Ò³Èý</span>
<div id="div1">¿éÒ»</div><div id="div2">¿é¶þ</div><div id="div3">¿éÈý</div>
£¨2£© ÉèÖñê¸÷<span>ºÍ<div>µÄÏÔʾ·½Ê½£ºÖ÷Òª½«div1µÄdisplayÊôÐÔÉèΪblock£¬div2ºÍdiv3µÄdisplayÊôÐÔÉèΪnone¡£¼´Ä¬ÈÏÏÔʾdiv1¡£
£¨3£© ÈÃspanµÄonmousemoveÏÔʾ¸÷×ԵıêÇ©¿é¡£È磺<span id="span1" onmousemove="showdiv(0)"> <span id="span2" onmousemove="showdiv(1)"> <span id="span3" onmousemove="showdiv(2)">
£¨4£© ÓÃjavascriptʵÏÖshowdiv(i)º¯Êý£º
Ê×ÏȽ¨Á¢Á½ ......
ÔÚÍøÉÏÕÒµ½ÔÚIEϲÙ×÷IFrameÄÚÈݵĴúÂë:
document.frames["MyIFrame"].document.getElementById("s").style.color="blue";
µ«ÊÇÕâÔÚFirefoxÏÂÎÞЧ¡£
ËùÒÔ£¬Ïëµ½ÔÚFirefoxÏÂÓÃFireBugÀ´µ÷ÊÔ¡£¾¹ýµ÷ÊÔ·¢ÏÖÔÚFirefoxÏ¿ÉÓÃÒÔÏ´úÂëÀ´ÊµÏÖ:
document.getElementById("MyIFrame").contentDocument.getElementById("s").style.color="blue";
Ïêϸ´úÂëÈçÏÂ:
TestIFrame.htm:
<html>
<head>
<script type="text/javascript">
function f(){
var doc;
if (document.all){//IE
doc = document.frames["MyIFrame"].document;
}else{//Firefox
doc = document.getElementById("MyIFrame").contentDocument;
&n ......