js常見的繼承方式

js常見的繼承方式

1. 原型鏈繼承

function Parent1() {
    this.name = 'parent1';
    this.play = [1,2,3];
}
function Child1() {
    this.type = 'child2'
}
Child1.prototype = new Parent1()
const child1 = new Child1();
const child2 = new Child1();
child1.play.push(3)
原型鏈繼承

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

function Parent1() {
    this.name = 'parent1'
}
Parent1.prototype.getName = function() {
    return this.name
}
function Child1() {
    Parent1.call(this)
    this.type = 'child1'
}
const child1 = new Child1()
console.log(child1);
console.log(child1.getName);
構(gòu)造函數(shù)繼承

3. 組合式繼承

function Parent1() {
    this.name = 'parent1';
    this.type = [1,2,3]
}
Parent1.prototype.say = function() {
    return this.attr
}
function Child1() {
    // 第二次執(zhí)行
    Parent1.call(this)
    this.attr = 'child1';
}
// 第一次執(zhí)行
Child1.prototype = new Parent1();
/* 修改構(gòu)造函數(shù)指向 */
Child1.prototype.constructor = Child1;
const child1 = new Child1();
const child2 = new Child1();
child1.type.push(1)
console.log(child1);
console.log(child2);
console.log(child1.say());
console.log(child2.say());

組合式繼承

4. 原型式繼承

const Parent1 = {
    name: '張三',
    list: [1,2,3],
    getName: function() {
        return this.name
    }
}
const child1 = Object.create(Parent1)
const child2 = Object.create(Parent1)
child1.list.push(1)
child1.name = '亞索'
console.log(child1.name);
console.log(child1.getName());
console.log(child2.name);
console.log(child1.list);
console.log(child2.list);
原型式繼承

原型式繼承

5. 寄生式繼承

const Parent1 = {
    name: '張三',
    list: [1,2,3],
    getName: function() {
        return this.name
    }
}
function clone(parent) {
    const clone = Object.create(parent)
    clone.getList = function() {
        return this.list
    }
    return clone
}
const child1 = clone(Parent1)
const child2 = clone(Parent1)
child1.name = 'child1'
console.log(child1.name);
console.log(child2.name);
console.log(child1.getName());
console.log(child2.getName());
console.log(child1.getList());

/*********************傳統(tǒng)寄生式繼承************************/
// 設(shè)置父類自有屬性和方法
let parent2 = {
    name: 'zy',
    hobbies: ['tennis', 'music', 'photography'],
    getName: function () { console.log(this.name) }
}
// 這個方法用于創(chuàng)建一個新對象并且連接原型鏈
function object(obj) {
    function F() { }
    F.prototype = obj;
    return new F();
}
function createson(o, sex) {
    // 傳入父類創(chuàng)建個新對象  
    let newson = object(o)
    // 這里增強(qiáng)對象,添加屬性和方法
    newson.sex = sex
    newson.getsex = function () { console.log(this.sex) }
    // 返回對象
    return newson
}
let sub2 = createson(parent2, 'famle')
console.log(sub2)
sub2.getName()
sub2.getsex()
寄生式繼承

6. 寄生組合式繼承

 function Parent1() {
    this.name = 'Parent1',
    this.list = [1,2,3]
}
Parent1.prototype.getList = function() {
    return this.list
}
function Child1() {
    Parent1.call(this)
}
Child1.prototype = Object.create(Parent1.prototype);
Child1.prototype.construct = Child1
const child1 = new Child1()
const child2 = new Child1()
const child3 = new Child1()
child1.list.push(1)
child1.name = 'child1'
console.log(child1.name);
console.log(child2.name);
console.log(child1.getList());
console.log(child2.getList());
寄生組合式繼承
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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