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

js 覆蓋和重載 函數(shù)

學(xué)過Java的人對(duì)函數(shù)的覆蓋和重載肯定是再熟悉不過了。
重載指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值不一樣。
覆蓋指兩個(gè)或多個(gè)函數(shù)的參數(shù)類型,順序和數(shù)量以及返回值完全一樣。
Javascript真的有這種特性么?
回答是JS中函數(shù)重名只會(huì)采用最后一個(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;
    };
    //測(cè)試同名方法
    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測(cè)試,交換兩個(gè)函數(shù)的順序
    //測(cè)試同名方法
    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測(cè)試,測(cè)試覆蓋,同名同參數(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 的按鈕

頁(yè)面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is undefined
然后按名為 function with one argument test 的按鈕
頁(yè)面的結(jié)果為 this is a function named 'testFun' with one argument,the argument is test
然后按名為 function with no arguments 的按鈕
頁(yè)面的結(jié)果為 this is a function named 'testFun2' with no arguments.
然后按名為 function with one argument test 的按鈕
頁(yè)面的結(jié)果為 this is a function named 'testFun2' with no arguments.

從以上的測(cè)試中我們發(fā)現(xiàn)我們只是點(diǎn)換了兩個(gè)函數(shù)的定義順序,結(jié)果大不相同。
從上面的測(cè)試中我們可以得出結(jié)論: 重載的話,只要函數(shù)定義在下面就會(huì)覆蓋上面的函數(shù)定義。
好了,接下來看覆蓋。
按名為 test function share the same name and arguments. 的按鈕

頁(yè)面的結(jié)果為 this is a function named 'testFun3' second.
測(cè)試結(jié)果很明顯,結(jié)論也是和上面相同的。
最終,我們得出結(jié)論:
方法重名,JS會(huì)以最后定義的函數(shù)作為函數(shù)體。當(dāng)然這不包括JS中的繼承中的覆蓋。
歡迎拍磚

JavaScript技術(shù)js 覆蓋和重載 函數(shù),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 仁爱版九年级英语上册教案| 村暖花开| 家庭理论电影| 扭曲的欲望| 色戒在线观看汤唯| 扭扭棒卡皮巴拉教程| 好好说再见| 唐朝诡事录最大败笔是谁| 姬培杰| 来生缘吉他谱c调| 女同性恋视频网站| 宋小宝电影| 松永| 韩绛| 木偶人| 黄瓜在线| 西藏卫视| 二年级上册数学竖式计算题| 爱的替身| 高登眼镜| 茅山道士在线观看| 拾贝的小女孩阅读理解答案 | 邓稼先教学设计一等奖优秀教案| 大村官之放飞梦想| 爱情最美丽 电视剧| 埃尔加,她狼| 经典常谈阅读笔记| 丧尸童子军| 釜山电影节| 国产伦理女村支书| xxxxxxxxxxxxx| 巨乳姐妹| 坏老师| 德国老太性视频播放| 新力量电影在线观看免费| 江苏卫视节目预告| 普罗米修斯 电影| www.douyin.com| 凯特摩丝| 吻船| 特种部队全面反击|