js 覆蓋和重載 函數(shù)
更新時間:2009年09月25日 22:09:34 作者:
學過JAVA的人對函數(shù)的覆蓋和重載肯定是再熟悉不過了。
學過JAVA的人對函數(shù)的覆蓋和重載肯定是再熟悉不過了。
重載指兩個或多個函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值不一樣。
覆蓋指兩個或多個函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值完全一樣。
那javascript真的有這種特性么?
回答是JS中函數(shù)重名只會采用最后一個定義。
首先來看下下面的代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
//展現(xiàn)結果
function showResult(result) {
var showDiv = document.getElementById('result');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結果2
function showResult2(result) {
var showDiv = document.getElementById('result2');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結果3
function showResult3(result) {
var showDiv = document.getElementById('result3');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//測試同名方法
function testFun() {
showResult('this is a function named \'testFun\' with no arguments.');
};
function testFun(arg) {
showResult('this is a function named \'testFun\' with one argument,the argument is '+arg);
};
//2th測試,交換兩個函數(shù)的順序
//測試同名方法
function testFun2(arg) {
showResult2('this is a function named \'testFun2\' with one argument,the argument is '+arg);
};
function testFun2() {
showResult2('this is a function named \'testFun2\' with no arguments.');
};
//3th測試,測試覆蓋,同名同參數(shù)
function testFun3() {
showResult3('this is a function named \'testFun3\' first.');
};
function testFun3() {
showResult3('this is a function named \'testFun3\' second.');
};
//-->
</SCRIPT>
<BODY>
<div>
<input type='button' onclick='testFun();' value='function with no arguments'/></br>
<input type='button' onclick="testFun('test');" value='function with one argument test'/>
</div>
<div id="result"></div>
<hr>2th test <hr>
<div>
<input type='button' onclick='testFun2();' value='function with no arguments'/></br>
<input type='button' onclick="testFun2('test');" value='function with one argument test'/>
</div>
<div id="result2"></div>
<hr>3th test <hr>
<div>
<input type='button' onclick='testFun3();' value='test function share the same name and arguments.'/></br>
</div>
<div id="result3"></div>
</BODY>
</HTML>
首先按名為 function with no arguments 的按鈕
頁面的結果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
從以上的測試中我們發(fā)現(xiàn)我們只是點換了兩個函數(shù)的定義順序,結果大不相同。
從上面的測試中我們可以得出結論: 重載的話,只要函數(shù)定義在下面就會覆蓋上面的函數(shù)定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕
頁面的結果為 this is a function named 'testFun3' second.
測試結果很明顯,結論也是和上面相同的。
最終,我們得出結論:
方法重名,JS會以最后定義的函數(shù)作為函數(shù)體。當然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
重載指兩個或多個函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值不一樣。
覆蓋指兩個或多個函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值完全一樣。
那javascript真的有這種特性么?
回答是JS中函數(shù)重名只會采用最后一個定義。
首先來看下下面的代碼
復制代碼 代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
//展現(xiàn)結果
function showResult(result) {
var showDiv = document.getElementById('result');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結果2
function showResult2(result) {
var showDiv = document.getElementById('result2');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結果3
function showResult3(result) {
var showDiv = document.getElementById('result3');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//測試同名方法
function testFun() {
showResult('this is a function named \'testFun\' with no arguments.');
};
function testFun(arg) {
showResult('this is a function named \'testFun\' with one argument,the argument is '+arg);
};
//2th測試,交換兩個函數(shù)的順序
//測試同名方法
function testFun2(arg) {
showResult2('this is a function named \'testFun2\' with one argument,the argument is '+arg);
};
function testFun2() {
showResult2('this is a function named \'testFun2\' with no arguments.');
};
//3th測試,測試覆蓋,同名同參數(shù)
function testFun3() {
showResult3('this is a function named \'testFun3\' first.');
};
function testFun3() {
showResult3('this is a function named \'testFun3\' second.');
};
//-->
</SCRIPT>
<BODY>
<div>
<input type='button' onclick='testFun();' value='function with no arguments'/></br>
<input type='button' onclick="testFun('test');" value='function with one argument test'/>
</div>
<div id="result"></div>
<hr>2th test <hr>
<div>
<input type='button' onclick='testFun2();' value='function with no arguments'/></br>
<input type='button' onclick="testFun2('test');" value='function with one argument test'/>
</div>
<div id="result2"></div>
<hr>3th test <hr>
<div>
<input type='button' onclick='testFun3();' value='test function share the same name and arguments.'/></br>
</div>
<div id="result3"></div>
</BODY>
</HTML>
首先按名為 function with no arguments 的按鈕
頁面的結果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁面的結果為 this is a function named 'testFun2' with no arguments.
從以上的測試中我們發(fā)現(xiàn)我們只是點換了兩個函數(shù)的定義順序,結果大不相同。
從上面的測試中我們可以得出結論: 重載的話,只要函數(shù)定義在下面就會覆蓋上面的函數(shù)定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕
頁面的結果為 this is a function named 'testFun3' second.
測試結果很明顯,結論也是和上面相同的。
最終,我們得出結論:
方法重名,JS會以最后定義的函數(shù)作為函數(shù)體。當然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
您可能感興趣的文章:
相關文章
javascript字體顏色控件的開發(fā) JS實現(xiàn)字體控制
小編給大家?guī)硪粋€用javascript編寫的能控制字體大小個顏色等基本信息的控件寫法,喜歡的嘗試編寫一下。2017-11-11
微信小程序實現(xiàn)點擊按鈕修改文字大小功能【附demo源碼下載】
這篇文章主要介紹了微信小程序實現(xiàn)點擊按鈕修改文字大小功能,涉及微信小程序事件綁定及setData動態(tài)修改Page頁面data數(shù)據(jù),進而控制頁面元素屬性動態(tài)改變的相關操作技巧,需要的朋友可以參考下2017-12-12
javascript基于prototype實現(xiàn)類似OOP繼承的方法
這篇文章主要介紹了javascript基于prototype實現(xiàn)類似OOP繼承的方法,實例分析了JavaScript使用prototype實現(xiàn)面向對象程序設計的中類繼承的相關技巧,需要的朋友可以參考下2015-12-12
javascript得到XML某節(jié)點的子節(jié)點個數(shù)的腳本
得到XML某節(jié)點(pnode)的子節(jié)點個數(shù)(客戶端)2008-10-10

