JS的多种继承方式和多继承

本文深入探讨了JavaScript中的各种继承方式,包括原型链继承、构造函数继承、组合继承、util.inherits继承、类继承以及原型链多继承。每种方式都详细讲解了其实现原理、特点及局限性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//JS中的继承

/**
 * @event 原型链实现继承
 * @description 利用原型让一个引用类型继承另一个引用类型的属性和方法,就是让一个原型对象等于另一个类型的实例
 * @question 不能向父类的构造函数传递参数
 */

function father() {
    this.name = "父亲";
};
father.prototype.method = function () {
    return this.name;
};

function son() {
    this.names = "孩子"
};
son.prototype = new father(); //继承
son.prototype.son_method = function () {
    return this.names;
};

const func_son = new son(); //初始化对象
const func_fat = new father(); //初始化对象
console.log(func_son.method(), func_son.son_method());


/**
 * @event 构造函数继承
 * @description 在子构造函数中,使用call方法执行父构造函数
 * @question 无法复用父类方法
 */

function father_cons() {
    this.desc = "父类描述哈哈哈"
};
father_cons.prototype.method = function () {
    return this.desc;
};

function son_cons() {
    father_cons.call(this);
};
const func_son_ = new son_cons();
const func_fat_ = new father_cons();
son_cons.prototype.descMethod = function () {
    return this.desc;
};
console.log(func_son_.descMethod());


/**
 * @event 组合继承
 * @description 将原型链集成和构造函数继承组合在一起
 * @question 步骤过于繁琐
 */

function __father() {
    this.f_name = "父亲";
    this.f_age = 55;
}
__father.prototype._method = function () {
    return this.f_name + this.f_age;
};

function __son() {
    this.s_name = "孩子";
    this.s_age = 22;
};
__son.prototype = new __father();
__son.prototype.constructor = __son;
__son.prototype._son_method = function () {
    return this.s_name + this.f_age;
};
const __son_const = new __son();
console.log(__son_const._son_method());


/**
 * @event util.inherits组合继承
 * @description 使用node工具包完成继承,inherits包含两个参数 第一个参数为子函数,第二个参数为父函数
 * @question 步骤繁琐.. 感觉跟用组合继承差不多
 */

const util = require("util");

function util_father() {
    this.f_name = "父亲";
    this.f_age = 55;
    this.f_address = "北京海淀"
};

util_father.prototype.f_method = function () {
    return this.f_name + this.f_age + this.f_address;
};

function util_son() {
    this.s_name = "孩子";
    this.s_age = 22;
    this.s_address = "北京朝阳";
    util_father.call(this); //继承父函数的属性
};

util_son.prototype.s_method = function () {
    return this.s_name + this.f_age + this.s_address;
};

util.inherits(util_son, util_father); //继承父函数的方法

const ut_son = new util_son();
console.log(ut_son.f_method());


/**
 * @event 类继承
 * @description 使用class和extends关键字,es5继承的语法糖
 * @question 清晰
 */

class father_class {
    constructor() {
        this.name = "zzw";
        this.age = 22;
        this.home = "beijing"   
    }
    one() {
        return this.name + this.age + this.home
    };
}

class son_class extends father_class {
    constructor() {
        super();
        this.address = "北京朝阳";
    }
    two() {
        return this.name + this.age + this.home + this.address;
    }
}

const __son_class = new son_class();
console.log(__son_class.one());


/**
 * @event 原型链多继承
 * @description 使用原型链的方式完成多继承
 * @question 很少有人这么用
 */

function more_father() {
    this.f_name = "父亲";
    this.f_age = 55;
    this.f_address = "北京朝阳"
};

more_father.prototype.f_method = function () {
    return this.f_name + this.f_age + this.f_address;  
};

function more_mother() {
    this.m_name = "母亲";
    this.m_age = 53;
    this.m_address = "北京朝阳";
};

more_mother.prototype.m_method = function () {
    return this.m_name + this.m_age + this.m_address;  
};


function more_son() {
    this.s_name = "孩子";
    this.s_age = 22;
    this.s_address = "北京朝阳"
};

more_son.prototype.s_method = function () {
    return this.s_name + this.s_age + this.s_address;  
};

more_son.prototype.f_pro = new more_father();
more_son.prototype.m_pro = new more_mother();
const _mores = new more_son();
console.log(_mores.f_pro.f_method())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值