前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS篇(010)-JavaScript 继承的方式和优缺点

JS篇(010)-JavaScript 继承的方式和优缺点

作者头像
齐丶先丶森
发布2022-05-12 20:07:26
3770
发布2022-05-12 20:07:26
举报
文章被收录于专栏:前端面试秘籍

答案:六种方式

一、原型链继承

代码语言:javascript
复制
function Parent () {
    this.name = 'kevin';
}

Parent.prototype.getName = function () {
    console.log(this.name);
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

console.log(child1.getName()) // kevin

缺点: 1. 引用类型的属性被所有实例共享

代码语言:javascript
复制
function Parent () {
    this.names = ['kevin', 'daisy'];
}

function Child () {

}

Child.prototype = new Parent();

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy", "yayu"]

2. 在创建 Child 的实例时,不能向 Parent 传参 二、借用构造函数(经典继承)

代码语言:javascript
复制
function Parent () {
    this.names = ['kevin', 'daisy'];
}

function Child () {
    Parent.call(this);
}

var child1 = new Child();

child1.names.push('yayu');

console.log(child1.names); // ["kevin", "daisy", "yayu"]

var child2 = new Child();

console.log(child2.names); // ["kevin", "daisy"]

优点: 1. 避免了引用类型的属性被所有实例共享 2. 可以在 Child 中向 Parent 传参

代码语言:javascript
复制
function Parent (name) {
    this.name = name;
}

function Child (name) {
    Parent.call(this, name);
}

var child1 = new Child('kevin');

console.log(child1.name); // kevin

var child2 = new Child('daisy');

console.log(child2.name); // daisy

缺点: 1. 方法都在构造函数中定义,每次创建实例都会创建一遍方法。

三、组合继承

代码语言:javascript
复制
function Parent (name) {
    this.name = name;
    this.colors = ['red', 'blue', 'green'];
}

Parent.prototype.getName = function () {
    console.log(this.name)
}

function Child (name, age) {

    Parent.call(this, name);
    
    this.age = age;

}

Child.prototype = new Parent();

var child1 = new Child('kevin', '18');

child1.colors.push('black');

console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]

var child2 = new Child('daisy', '20');

console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]

优点: 1. 融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。 四、原型式继承

代码语言:javascript
复制
function createObj(o) {
    function F(){}
    F.prototype = o;
    return new F();
}

缺点: 1. 包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。 五、寄生式继承

代码语言:javascript
复制
function createObj (o) {
    var clone = object.create(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}

缺点: 1. 跟借用构造函数模式一样,每次创建对象都会创建一遍方法。 六、寄生组合式继承

代码语言:javascript
复制
function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

function prototype(child, parent) {
    var prototype = object(parent.prototype);
    prototype.constructor = child;
    child.prototype = prototype;
}

// 当我们使用的时候:
prototype(Child, Parent);

优点: 1. 这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent. prototype 上面创建不必要的、多余的属性。 2. 与此同时,原型链还能保持不变; 3. 因此,还能够正常使用 instanceof 和 isPrototypeOf。 开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式 [参与互动](https://github.com/yisainan/web-interview/issues/179)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 前端面试秘籍 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档