JS-类的继承方式

当我们了解了原型和原型链后我们就可以来看JS类的继承方式
不了解的可以点这里:原型与原型链

原型链继承

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

缺点:这种继承方式引用类型的属性被所有实例共享,在创建Child的时候不能传参

function Parent () {
        this.name=['kevin','dasiy'];
}
function Child () {
}
Child.prototype = new Parent();
var child1 = new Child();
var child2 = new Child();
child1.name.push('张三');
console.log(child1.name); //   ["kevin", "daisy", "张三"]
console.log(child2.name); // ["kevin", "daisy", "张三"]

使用构造函数(经典继承)

优点:避免了引用类型的属性被所有实例共享,可以在Child中向Parent传值
缺点:方法都在构造函数中定义,每次创建实例都会创建一遍方法

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('jason');
console.log (child2.name); // jason

组合继承(原型链继承+经典继承)

function Parent(name) {
      this.name = name;
      this.color = ['red','yellow','green'];
}
Parent.prototype.getName = function () {
      console.log(this.name);
}
function Child (name,age) {
     Parent.call(this,name);
     this.age = age;
}
Child.prototype = new Parent ();
Child.prototype.constructor = Child;
var child1 = new Child('kevin', '18');
child1.color.push('black');
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.color); // ["red", "blue", "green", "black"]
var child2 = new Child('daisy', '20');
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.color); // ["red", "blue", "green"]

优点:融合了原型链继承的优点和经典继承的优点
注意:组合继承最大的缺点是会调用两次父构造函数。
第一次:Child.prototype = new Parent();
第二次:var child1 = new Child('kevin', '18');

原型式继承

function createObj(o) {
        function F () {}
        F.prototype = o;
        return new F();
}
var person = {
      name: 'kevin',
      friends: ['daisy','kelly']
}
var person1 = createObj(person);
var person2 = createObj(person);
person1.name = 'person1';
console.log(person2.name); //kevin
person1.friends.push('taylor');
console.log(person2.friends); //["daisy", "kelly", "taylor"]

缺点:引用类型的值始终会被共享

寄生式继承

创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。

function createObj (o) {
    var clone = Object.create(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}

缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。

寄生组合继承

解决组合继承需要调用两次父构造函数的缺点

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值