茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    JavaScript模擬實現(xiàn)繼承的方法
    來源:易賢網(wǎng) 閱讀:840 次 日期:2015-04-01 16:42:59
    溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript模擬實現(xiàn)繼承的方法”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了JavaScript模擬實現(xiàn)繼承的方法,實例分析了javascript類的操作與模擬實現(xiàn)繼承的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

    本文實例講述了JavaScript模擬實現(xiàn)繼承的方法。分享給大家供大家參考。具體分析如下:

    我們都知道,在JavaScript中只能模擬實現(xiàn)OO中的"類",也就意味著,在JavaScript中沒有類的繼承。我們也只能通過在原對象里添加或改寫屬性來模擬實現(xiàn)。

    先定義一個父類,

    //父類

    function ParentClass() {

    this.className = "ParentClass";

    this.auth = "Auth";

    this.version = "V1.0";

    this.parentClassInfo = function () {

    return this.className + "\n" + this.auth + "\n" + this.version;

    }

    }

    一、prototype 實現(xiàn):

    //子類

    //1、prototype繼承

    function ChildClassByPrototype() {

    this.date = "2013-07-26";

    this.classInfo = function () {

    return this.parentClassInfo() + "\n" + this.date;

    }

    }

    ChildClassByPrototype.prototype = new ParentClass();

    var cctest1 = new ChildClassByPrototype();

    cctest1.parentClassInfo();

    cctest1.classInfo();

    這種方式很簡單,只需把父類的實例賦值給子類的prototype屬性就行了,然后子類就可以使用父親的方法和屬性了。這里其實是用到了原型鏈向上查找的特性,比如這個例子中的 cctest1.parentClassInfo() 方法,JavaScript會先在ChildClassByPrototype的實例中查找是否有parentClassInfo()方法,子類中沒有,所以繼續(xù)查找ChildClassByPrototype.prototype屬性,而其prototype屬性的值是ParentClass的一個實例,該實例有parentClassInfo()方法,于是查找結(jié)束,調(diào)用成功。

    二、apply 實現(xiàn):

    //2、apply繼承

    function ChildClassByApply() {

    ParentClass.apply(this, new Array());

    //ParentClass.apply(this, []);

    this.date = "2013-07-26";

    this.classInfo = function () {

    return this.parentClassInfo() + "\n" + this.date;

    }

    }

    JavaScript中的apply可以理解為用A方法替換B方法,第一個參數(shù)為B方法的對象本身,第二個參數(shù)為一個數(shù)組,該數(shù)組內(nèi)的值為需要傳遞給A方法對應(yīng)的參數(shù)列表,如果參數(shù)為空,即沒有參數(shù)傳遞,可通過 new Array()、[] 來傳遞。

    三、call + prototype 實現(xiàn):

    //3、call+prototype繼承

    function ChildClassByCall() {

    ParentClass.call(this, arguments);

    this.date = "2013-07-26";

    this.classInfo = function () {

    return this.parentClassInfo() + "\n" + this.date;

    }

    }

    ChildClassByCall.prototype = new ParentClass();

    call和apply作用類似,即都是用A方法替換B方法,只是傳遞的參數(shù)不一樣,call方法的第一個參數(shù)為B方法的對象本身,后續(xù)的參數(shù)則不用Array包裝,需直接依次進行傳遞。既然作用差不多,為何多了一句 原型賦值呢?這是因為call方法只實現(xiàn)了方法的替換而沒有對對象屬性進行復(fù)制操作。

    每種方法都有其適用環(huán)境,比如,如果父類帶有有參構(gòu)造函數(shù):

    function ParentClass(className, auth, version) {

    this.className = className;

    this.auth = auth;

    this.version = version;

    this.parentClassInfo = function () {

    return this.className + "\n" + this.auth + "\n" + this.version;

    }

    }

    這種情況下,prototype就不適用了,可選用apply或call;

    function ChildClassByApply(className, auth, version) {

    ParentClass.apply(this, [className, auth, version]);

    this.date = "2013-07-26";

    this.classInfo = function () {

    return this.parentClassInfo() + "\n" + this.date;

    }

    }

    function ChildClassByCall(className, auth, version) {

    ParentClass.call(this, arguments[0], arguments[1], arguments[2]);

    //ParentClass.call(this, className, auth, version);

    this.date = "2013-07-26";

    this.classInfo = function () {

    return this.parentClassInfo() + "\n" + this.date;

    }

    }

    ChildClassByCall.prototype = new ParentClass();

    實例化:

    var cctest2 = new ChildClassByApply("ParentClass", "Auth", "V1.0");

    var cctest3 = new ChildClassByCall("ParentClass", "Auth", "V1.0");

    在apply和call中,又該如何取舍呢?在OO的繼承中,子類繼承于父類,那么它應(yīng)該也是父類的類型。即,ChildClassByCall、ChildClassByApply應(yīng)該也是ParentClass類型,但我們用"instanceof"檢測一下就會發(fā)現(xiàn),通過apply繼承的子類,并非ParentClass類型。所以,我們建議用call + prototype 來模擬實現(xiàn)繼承。據(jù)說,Google Map API 的繼承就是使用這種方式喲。

    希望本文所述對大家的javascript程序設(shè)計有所幫助。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看腳本欄目
    易賢網(wǎng)手機網(wǎng)站地址:JavaScript模擬實現(xiàn)繼承的方法

    2026國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)