js 覆蓋和重載 函數(shù)
更新時(shí)間:2009年09月25日 22:09:34 作者:
學(xué)過JAVA的人對函數(shù)的覆蓋和重載肯定是再熟悉不過了。
學(xué)過JAVA的人對函數(shù)的覆蓋和重載肯定是再熟悉不過了。
重載指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值不一樣。
覆蓋指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值完全一樣。
那javascript真的有這種特性么?
回答是JS中函數(shù)重名只會采用最后一個(gè)定義。
首先來看下下面的代碼
<!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)結(jié)果
function showResult(result) {
var showDiv = document.getElementById('result');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結(jié)果2
function showResult2(result) {
var showDiv = document.getElementById('result2');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結(jié)果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測試,交換兩個(gè)函數(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 的按鈕
頁面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁面的結(jié)果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁面的結(jié)果為 this is a function named 'testFun2' with no arguments.
從以上的測試中我們發(fā)現(xiàn)我們只是點(diǎn)換了兩個(gè)函數(shù)的定義順序,結(jié)果大不相同。
從上面的測試中我們可以得出結(jié)論: 重載的話,只要函數(shù)定義在下面就會覆蓋上面的函數(shù)定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕
頁面的結(jié)果為 this is a function named 'testFun3' second.
測試結(jié)果很明顯,結(jié)論也是和上面相同的。
最終,我們得出結(jié)論:
方法重名,JS會以最后定義的函數(shù)作為函數(shù)體。當(dāng)然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
重載指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值不一樣。
覆蓋指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值完全一樣。
那javascript真的有這種特性么?
回答是JS中函數(shù)重名只會采用最后一個(gè)定義。
首先來看下下面的代碼
復(fù)制代碼 代碼如下:
<!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)結(jié)果
function showResult(result) {
var showDiv = document.getElementById('result');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結(jié)果2
function showResult2(result) {
var showDiv = document.getElementById('result2');
showDiv.innerHTML = '';
showDiv.innerHTML = result;
};
//展現(xiàn)結(jié)果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測試,交換兩個(gè)函數(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 的按鈕
頁面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁面的結(jié)果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁面的結(jié)果為 this is a function named 'testFun2' with no arguments.
從以上的測試中我們發(fā)現(xiàn)我們只是點(diǎn)換了兩個(gè)函數(shù)的定義順序,結(jié)果大不相同。
從上面的測試中我們可以得出結(jié)論: 重載的話,只要函數(shù)定義在下面就會覆蓋上面的函數(shù)定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕
頁面的結(jié)果為 this is a function named 'testFun3' second.
測試結(jié)果很明顯,結(jié)論也是和上面相同的。
最終,我們得出結(jié)論:
方法重名,JS會以最后定義的函數(shù)作為函數(shù)體。當(dāng)然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
您可能感興趣的文章:
- 如何實(shí)現(xiàn)JS函數(shù)的重載
- js中方法重載如何實(shí)現(xiàn)?以及函數(shù)的參數(shù)問題
- JS函數(shù)重載的解決方案
- 有關(guān)于JS構(gòu)造函數(shù)的重載和工廠方法
- Javascript基礎(chǔ) 函數(shù)“重載” 詳細(xì)介紹
- 詳解JS函數(shù)重載
- 為JavaScript添加重載函數(shù)的輔助方法
- JavaScript中的函數(shù)重載深入理解
- 添加JavaScript重載函數(shù)的輔助方法2
- javascript函數(shù)重載解決方案分享
- 通過實(shí)例理解javascript中沒有函數(shù)重載的概念
- JavaScript函數(shù)重載操作實(shí)例淺析
相關(guān)文章
JS驗(yàn)證輸入的是否是數(shù)字及保留幾位小數(shù)問題
這篇文章主要介紹了JS驗(yàn)證輸入的是否是數(shù)字及保留幾位小數(shù)問題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問題解決
本文主要介紹了Echarts中X軸/Y軸坐標(biāo)標(biāo)簽顯示不下的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
全面了解javascript中的錯(cuò)誤處理機(jī)制
下面小編就為大家?guī)硪黄媪私鈐avascript中的錯(cuò)誤處理機(jī)制。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07
詳解使用JWT實(shí)現(xiàn)單點(diǎn)登錄(完全跨域方案)
這篇文章主要介紹了詳解使用JWT實(shí)現(xiàn)單點(diǎn)登錄(完全跨域方案),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
JavaScript惰性載入函數(shù)實(shí)例分析
這篇文章主要介紹了JavaScript惰性載入函數(shù),結(jié)合實(shí)例形式分析了JavaScript惰性載入函數(shù)的概念、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03

