繼承

1. 構(gòu)造函數(shù)繼承

 問題:原型上的方法或者屬性,無(wú)法繼承
    function Fn() {
        this.name = "zhangsan",
        this.age = 12
        this.eat = function () {
        console.log("eat")
        }
    }
    Fn.prototype.sleep = function () {
        console.log("sleep")
    } // 無(wú)法繼承
    function F() {
        // console.log(this)
        // Array.prototype.join.call(obj,'-') / Array.prototype.join.apply(obj,['-'])
        Fn.call(this)
    }
        var fn = new Fn()
        console.log(fn)
        var f = new F()
        console.log(f.sleep())

重點(diǎn):用.call()和.apply()將父類構(gòu)造函數(shù)引入子類函數(shù)(在子類函數(shù)中做了父類函數(shù)的自執(zhí)行(復(fù)制))

特點(diǎn):1、只繼承了父類構(gòu)造函數(shù)的屬性,沒有繼承父類原型的屬性。

2、解決了原型鏈繼承缺點(diǎn)1、2、3。

3、可以繼承多個(gè)構(gòu)造函數(shù)屬性(call多個(gè))。

4、在子實(shí)例中可向父實(shí)例傳參。

缺點(diǎn):1、只能繼承父類構(gòu)造函數(shù)的屬性。

2、無(wú)法實(shí)現(xiàn)構(gòu)造函數(shù)的復(fù)用。(每次用每次都要重新調(diào)用)

3、每個(gè)新實(shí)例都有父類構(gòu)造函數(shù)的副本,臃腫。

2、 原型繼承

  問題: 共用一個(gè)原型對(duì)象,導(dǎo)致誰(shuí)修改原型對(duì)象的值,其余對(duì)象都會(huì)被更改
    function Fn() {
        this.name = "zhangsan"
        this.age = 12
        this.color = ["yellow", "pink"]
        this.eat = function () {
            console.log("eat")
        }
    }
    Fn.prototype.sleep = function () {
        console.log("sleep")
    }
    function F() {
    }
    F.prototype = new Fn()
    var f = new F()
    var f1 = new F()
    f.color.push("black")

    console.log(f1.color)

重點(diǎn):讓新實(shí)例的原型等于父類的實(shí)例。

特點(diǎn):1、實(shí)例可繼承的屬性有:實(shí)例的構(gòu)造函數(shù)的屬性,父類構(gòu)造函數(shù)屬性,父類原型的屬性。(新實(shí)例不會(huì)繼承父類實(shí)例的屬性?。?/p>

缺點(diǎn):1、新實(shí)例無(wú)法向父類構(gòu)造函數(shù)傳參。

2、繼承單一。

3、所有新實(shí)例都會(huì)共享父類實(shí)例的屬性。(原型上的屬性是共享的,一個(gè)實(shí)例修改了原型屬性,另一個(gè)實(shí)例的原型屬性也會(huì)被修改?。?/p>

3、組合方式繼承:

function Fn() {
    this.name = "zhangsan"
    this.age = 12
    this.color = ["yellow", "pink"]
    this.eat = function () {
        console.log("eat")
        }
    }
    function F() {
        Fn.call(this)
    }
    F.prototype = Object.create(Fn.prototype)
    // F.prototype = Fn.prototype.constructor === Fn
    F.prototype.constructor = F

    var f = new F()
    var f1 = new F()
    f.color.push("black")
    console.log(f1.color)

    function FCC() { }
    //instanceof 用來(lái)
    console.log(f instanceof FCC) //true
    console.log(f instanceof Fn)    //true
    console.log(typeof 1)       //numeber

重點(diǎn):結(jié)合了兩種模式的優(yōu)點(diǎn),傳參和復(fù)用

特點(diǎn):1、可以繼承父類原型上的屬性,可以傳參,可復(fù)用。

2、每個(gè)新實(shí)例引入的構(gòu)造函數(shù)屬性是私有的。

缺點(diǎn):調(diào)用了兩次父類構(gòu)造函數(shù)(耗內(nèi)存),子類的構(gòu)造函數(shù)會(huì)代替原型上的那個(gè)父類構(gòu)造函數(shù)。

  • instanceof運(yùn)算符的左邊是實(shí)例對(duì)象,右邊是構(gòu)造函數(shù)。它會(huì)檢查右邊構(gòu)建函數(shù)的原型對(duì)象(prototype),是否在左邊對(duì)象的原型鏈上。因此,上面兩種寫法是等價(jià)的。
  • instanceof運(yùn)算符只能用于對(duì)象,不適用原始類型的值。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容