四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

js 覆蓋和重載 函數

學過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中的繼承中的覆蓋。
歡迎拍磚

JavaScript技術js 覆蓋和重載 函數,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 高地1—36集电视剧免费观看剧情| hd经典复古性mature| 王芊| 天堂av| 文熙俊| 山上的小屋 残雪原文| 风间由美的作品| 艳女十八式无删减版| 苦菜花电视剧| 禁忌爱情| 吻胸亲乳激情大尺度| 韩国电影两个小姨子| 38在线电影| 电影在线观看高清完整版免费| 卢昱晓主演的电视剧| 宋学士濂文言文翻译| 男女视频在线播放| 沈月个人简历资料| 天台的月光| 十一码复式22块钱中奖对照表| 749局啥时候上映| 甲种公牛1976| 柏欣彤12点以后跳的广场舞| 龚子棋| 抖音最火的图片| 女友电影| 宅基地买卖合同协议书| 电影网1905免费版| 长恨歌电影| 电影《皮埃里诺》免费观看| angie faith| 奇骏车友会| 我们的快乐人生 电视剧| 从此以后歌词| 做菜的视频家常菜大全| 四三二一| 玉匣记全文免费| 合作协议书合同| 天下第一楼剧情介绍| 曹永廉| 欲盖弄潮电影|