实现继承的6种方式

面试过程中经常会有面试官问 『你知道继承吗』『你说说什么呢是原型链』感觉会,但是不知道怎么说。针对这个,系统的学习了下继承的几种方式,总结出来。

先理解 构造函数、原型、和实例的关系:
每一个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,实例包含一个指向原型对象的指针。让原型对象等于另一个类型的实例
此时原型对象将包含指向另一个原型的指针,则构成了原型链

1、原型链继承:
利用原型让一个引用类型继承另一个引用类型的属性和方法

function SuperType() {
    this.property = true;
}
SuperType.prototype.getSuperValue = function () {
   return this.property;
}
function SubType() {
    this.subProperty = false;
}
SubType.prototype = new SuperType();
SubType.prototype.constructor = SubType;
SubType.prototype.getSubValue = function () {
    return this.subProperty;
}
var instance = new SubType();
console.log(instance.constructor);

2、借用构造函数继承

function Father(name, age) {
    this.name = name;
    this.age = age;
    this.sayName = function() {
        return this.name;
    }
}

function Student() {
    Father.call(this, "lisi");
}

var student = new Student();
console.log(student.sayName());

3、组合继承

function Animals(name, age) {
    this.name = name;
    this.age = age;
    this.colors = ["red", "green"];
}
Animals.prototype.sayName = function(){
    return this.name;
}

Animals.prototype.sayAge = function(){
    return this.age;
}
Animals.prototype.sayColor = function(){
    return this.colors;
}

function Sheep() {
    Animals.call(this, "yang");
    this.age = 13;
}

Sheep.prototype = new Animals();
Sheep.prototype.constructor = Sheep;

var sheep = new Sheep();
sheep.colors.push("white");
console.log(Sheep.prototype.isPrototypeOf(sheep));
var sheep12 = new Sheep();
console.log(Animals.prototype.isPrototypeOf(sheep12));

4、原型式继承

function object(o) {
    function f() {}
    f.prototype = o;
    return new f();
}
var wutong = {
    name: "wutong",
    friends: ["shangze", "pingan"]
};
var anotherWutong = Object.create(wutong, {name: {
    value: "wutong1"
    }});
anotherWutong.name = "gren";
anotherWutong.friends.push("feifei");
console.log(anotherWutong);
var anotherWutong1 = Object.create(wutong);
console.log(anotherWutong1.name);

5、寄生式继承

function creatAnother(original) {
    var clone = Object.create(original);
    clone.sayHi = function() {
        console.log(this.name);
    }
    return clone;
}

var another = {
    name: "li9si",
    age: 23
}

var anotherPerson = creatAnother(another);
anotherPerson.sayHi();

6、寄生组合式继承

   function SuperType1(name) {
        this.name = name;
        this.age = 13;
        console.log(this);
    }
    SuperType1.prototype.sayName = function(){
        console.log(this.name);
    }
    function SubType1() {
        SuperType1.call(this, "lisi");
    }
    var obj = Object.create(SuperType1.prototype);
    SubType1.prototype = obj;
    SubType1.prototype.constructor = SubType1;
    var subtype1 = new SubType1();
    console.log(subtype1);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 原型链继承:通过将父类的实例作为子类的原型来实现继承实现方式:将子类的原型对象指向父类的实例对象,缺点是无法向父类构造函数传递参数。 2. 借用构造函数继承:通过在子类构造函数中调用父类构造函数来实现继承实现方式:在子类构造函数中调用父类构造函数,并使用call或apply方法改变this指向,缺点是无法继承父类原型链上的方法和属性。 3. 组合继承:通过将原型链继承和借用构造函数继承结合起来,实现同时继承父类属性和方法的继承方式实现方式:将子类的原型对象指向一个以父类实例为原型的对象,并在子类构造函数中调用父类构造函数,缺点是父类构造函数被调用了两次。 4. 原型式继承:通过对一个已有对象进行浅复制来实现继承实现方式:使用Object.create方法复制一个对象,并将其作为子类的原型对象,缺点是无法传递参数给父类构造函数。 5. 寄生式继承:通过在原型式继承的基础上,增强对象,实现继承实现方式:在原型式继承的基础上,增加一个封装继承过程的函数,返回一个增强后的对象,缺点是增加了复杂度。 6. 寄生组合式继承:通过组合继承和寄生式继承实现继承实现方式:在组合继承的基础上,使用寄生式继承,避免了父类构造函数被调用两次的问题,实现了最优的继承方式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值