国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js 覆蓋和重載 函數

2019-11-21 01:07:14
字體:
來源:轉載
供稿:網友
學過JAVA的人對函數的覆蓋和重載肯定是再熟悉不過了。
重載指兩個或多個函數的參數類型,順序和數量以及返回值不一樣。
覆蓋指兩個或多個函數的參數類型,順序和數量以及返回值完全一樣。
那javascript真的有這種特性么?
回答是JS中函數重名只會采用最后一個定義。
首先來看下下面的代碼
復制代碼 代碼如下:

<!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">
<!--

    
    //展現結果
    function showResult(result) {
        var showDiv = document.getElementById('result');
        showDiv.innerHTML = '';
        showDiv.innerHTML = result;
    };
    //展現結果2
    function showResult2(result) {
        var showDiv = document.getElementById('result2');
        showDiv.innerHTML = '';
        showDiv.innerHTML = result;
    };
    //展現結果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測試,交換兩個函數的順序
    //測試同名方法
    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測試,測試覆蓋,同名同參數
    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.

從以上的測試中我們發現我們只是點換了兩個函數的定義順序,結果大不相同。
從上面的測試中我們可以得出結論: 重載的話,只要函數定義在下面就會覆蓋上面的函數定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕

頁面的結果為 this is a function named 'testFun3' second.
測試結果很明顯,結論也是和上面相同的。
最終,我們得出結論:
方法重名,JS會以最后定義的函數作為函數體。當然這不包括JS中的繼承中的覆蓋。
歡迎拍磚
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹北市| 齐齐哈尔市| 合山市| 宝山区| 迭部县| 巴塘县| 建始县| 冀州市| 泸溪县| 平罗县| 西和县| 舟山市| 本溪市| 西丰县| 吴桥县| 江孜县| 上杭县| 台北县| 奈曼旗| 万州区| 马尔康县| 仪征市| 大冶市| 大化| 图木舒克市| 莱阳市| 宿州市| 闽侯县| 河间市| 白沙| 孝感市| 会理县| 松溪县| 顺昌县| 垫江县| 平顶山市| 永丰县| 汝南县| 北海市| 五大连池市| 和静县|