JavaScript的继承

方法1:通过原型链

这种模式的问题是Dog的所有实例共享同一个Animal实例,一旦在dog1种修改Animal属性,dog2种的Animal属性也被修改,这是我们不想看到的,因尔这种继承并不实用。

function Animal(){
    this.Age = null;
    this.Name = null;
}

Animal.prototype.Eat = function(){
    console.log(this.Name + ' eat')
}

function Dog(){
    this.Species = null;
}

Dog.prototype = new Animal();

var dog = new Dog();
dog.Eat()
console.log(dog);

方法2:通过call 改变this

但是这种方法仍然不完美, 每个Dog的实例中都有Eat方法,我们希望方法对于每个实例都是共享的,而不是每个实例都有一样的方法。

function Animal(age, name){
    this.Age = age;
    this.Name = name;
    this.Eat = function(){
        console.log(this.Name + ' eat');
    }
}

function Dog(species, age, name){
    this.Species = species;
    Animal.call(this, age, name);
}

var dog = new Dog('博美', '5', 'Jim');
console.log(dog);

方法3:组合继承模式

这种方法结合了方法1和方法2。用原型链实现方法的继承,在构造函数中实用call来实现属性的继承。这种方式基本完美,但是我们发现构造方法Animal被调用2次,对于追求精益求精的我们,希望有更加完美的实现。

function Animal(age, name){
    this.Age = age;
    this.Name = name;
}

Animal.prototype.Eat = function(){
    console.log(this.Name + ' eat');
}

function Dog(species, age, name){
    this.Species = species;
    Animal.call(this, age, name);   //第二次调用Animal()
}

Dog.prototype = new Animal();  // 第一次调用Animal()

var dog = new Dog('博美', '5', 'Jim');
dog.Eat();
console.log(dog);

方法4:寄生组合继承模式

属性通过call, 方法通过原型链,但是这种方法不会调用2次Animal(), 这种方式去掉了Animal中多余的Age和Name属性又完美的保留的与原型链。

// Object.create的非规范实现
function object(o) {
    function F() {}
    F.prototype = o;
    return new F();
}

function inheritPrototype(son, father) {
    var prototype = Object.create(father.prototype)
    // Object.create的实现参考上面的object

    prototype.constructor = son;
    // 通过Object.create生成的prototype没有constructor, 
    // 所以要弥补son一个constructor

    son.prototype = prototype;
   

   // ES6 种引入了Object.setPrototypeOf, 可以省略上面的代码直接用下面这行
   //Object.setPrototypeOf(son.prototype, father.prototype);
}

function Animal(age, name){
    this.Age = age;
    this.Name = name;
}

Animal.prototype.Eat = function() {
    console.log(this.Name + ' eat');
}

function Dog(species, age, name){
    this.Species = species;
    Animal.call(this, age, name);
}

inheritPrototype(Dog, Animal);

Dog.prototype.Cry = function(){
    console.log(this.Name + ' cry');
}

var dog = new Dog('博美', '5', 'Jim');
dog.Eat();
dog.Cry();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 方法1:通过原型链 这种模式的问题是Dog的所有实例共享同一个Animal实例,一旦在dog1种修改Animal属...
    强仔_5787阅读 101评论 0 0
  • 本文先对es6发布之前javascript各种继承实现方式进行深入的分析比较,然后再介绍es6中对类继承的支持以及...
    lazydu阅读 16,727评论 7 44
  • 继承有什么作用? 继承可以让子类拥有父类的方法和属性,然后在这个基础上进行方法和属性调用,可以提高代码的复用性和效...
    柏龙阅读 586评论 3 6
  • 在JS中继承是一个非常复杂的话题,比其他任何面向对象语言中的继承都复杂得多。在大多数其他面向对象语言中,继承一个类...
    殖民_FE阅读 201评论 0 2
  • //8.继承 构造函数方式继承 构造函数方式继承的缺点:构造函数的方法会在实例化的时候多次执行,没有必要。 原型方...
    一个废人阅读 77评论 0 2