JS方法重寫

from http://www.cnblogs.com/aooyu/archive/2009/12/11/1621904.htmljavascript


1.HTML的是被瀏覽器按順序解析的,那麼咱們看下面的例子 html

HTML code
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head > < script type ="text/javascript" src ="1.js" /> < script type = " text/javascript " > function fnTest(){ alert( " show me second! " ); } </ script > </ head > < body > < input type ="button" value ="clickMe" onclick ="fnTest()" /> </ body > </ html >


先引入了 <script type="text/javascript" src="1.js"/>以下: java

JScript code
function fnTest(){ alert( " show me first! " ); }


這時點擊clickMe彈出的是show me second!先不說明. 
咱們再看下面的例子:
 瀏覽器

HTML code
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head > < script type ="text/javascript" > function fnTest(){ alert( " show me second! " ); } </ script > < script type ="text/javascript" src ="1.js" /> < / head> < body > < input type = " button " value = " clickMe " onclick = " fnTest() "/ > < / body> < / html>


<script type="text/javascript" src="1.js"/>做爲後來引入的,以下: spa

JScript code
function fnTest(){ alert( " show me first! " ); }


這時點擊clickMe彈出的是show me first! 
這個很好理解,由於瀏覽器按順序解析的,先建立的fnTest()就被後建立的fnTest()覆蓋了.那麼這是爲何呢?咱們來看第二點: 
2.javascript方法實際上是個引用 code

JScript code
function fnTest(){ alert( " show me first! " ); } // 至關於: var fnTest = new Function( " alert('show me first!') " ); function fnTest(){ alert( " show me second! " ); } // 至關於: var fnTest = new Function( " alert('show me second!') " ); // 那麼結果是: var fnTest = new Function( " alert('show me first!') " ); var fnTest = new Function( " alert('show me second!') " );


最後就是按靠後調用的.先建立的方法將不被調用.獲得的是show me second!xml

相關文章
相關標籤/搜索