Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB
ÈÈÃűêÇ©£º c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 ×îÐÂÎÄÕ : javascript

Javascript±Õ°ü£¨Closure£©


±Õ°ü£¨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); ......

ÈçºÎ±ÜÃâJavascriptʼþ°ó¶¨³öÏÖÄÚ´æй©

"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±Õ°ü
 ×î½üÔÚÍøÉϲéÔÄÁ˲»ÉÙ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ÄÚ²¿£»
¡ ......

JavaScriptÏÔʾʱ¼ä

Õâ¸öÎÊÌâºÜ¼òµ¥£¬Ö÷ÒªÓÐÏÂÃ漸¸ö֪ʶµã£º
£¨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> ......

Javascript ÏÔʾ±êǩҳЧ¹û

ҪʵÏÖÕâ¸ö¹¦ÄܹؼüÊÇÒªÀí½â¿éµÄ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)º¯Êý£º
Ê×ÏȽ¨Á¢Á½ ......

javascript»ñÈ¡iframeÎĵµÄÚÈÝ(¼æÈÝIEºÍFirefox)

ÔÚÍøÉÏÕÒµ½ÔÚ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 ......
×ܼǼÊý:2244; ×ÜÒ³Êý:374; ÿҳ6 Ìõ; Ê×Ò³ ÉÏÒ»Ò³ [358] [359] [360] [361] 362 [363] [364] [365] [366] [367]  ÏÂÒ»Ò³ βҳ
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ