浅析js继承方式

来源:互联网 发布:剑三丐姐官方捏脸数据 编辑:程序博客网 时间:2024/06/06 03:29

前言

继承是指一个对象直接使用另一对象的属性和方法。在JS里,一切皆对象,JS作为面向对象的弱类型语言,继承也是其非常强大的特性之一。

js继承方式

要实现继承,我们得先有个父类,后面将以这个父类为基准实现继承,代码如下:

// 定义一个动物类function Animal (name) {    // 属性    this.name = name || 'Animal';    // 实例方法    this.speak = function(word){        console.log(this.name + ' is speaking: ' + word);    }}// Animal的原型方法Animal.prototype.eat = function(food) {    console.log(this.name + ' is eating:' + food);};

1. 原型链继承

核心:将父类的实例作为子类的原型

function Cat(){    this.name = 'cat';}Cat.prototype = new Animal();
//test codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); //trueconsole.log(cat instanceof Cat); //true

特点:

  1. 非常纯粹的继承关系,实例是子类的实例,也是父类的实例
  2. 父类新增原型方法/原型属性,子类都能访问到
  3. 简单,易于实现

缺点:

  1. 可以在Cat构造函数中,为Cat实例增加实例属性。如果要新增原型属性和方法,则必须放在new Animal()这样的语句之后执行。
  2. 无法实现多继承
  3. 来自原型对象的引用属性是所有实例共享的
  4. 创建子类实例时,无法向父类构造函数传参

2. 构造继承

核心:使用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类(没用到原型)

function Cat(){    Animal.call(this);    this.name = 'cat';}
//test codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miao//console.log(cat.eat("fish")); //会报错,没有eat方法console.log(cat instanceof Animal); //falseconsole.log(cat instanceof Cat); //true

特点:

  1. 解决了1中,子类实例共享父类引用属性的问题
  2. 创建子类实例时,可以向父类传递参数
  3. 可以实现多继承(call多个父类对象)

缺点:

  1. 实例并不是父类的实例,只是子类的实例
  2. 只能继承父类的实例属性和方法,不能继承原型属性/方法
  3. 无法实现函数复用,每个子类都有父类实例函数的副本,影响性能

3. 实例继承

核心:为父类实例添加新特性,作为子类实例返回

function Cat(name){    var instance = new Animal();    instance.name = name || 'cat';    return instance;}
//test codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); //trueconsole.log(cat instanceof Cat); //false

特点:

  1. 不限制调用方式,不管是new 子类()还是子类(),返回的对象具有相同的效果

缺点:

  1. 实例是父类的实例,不是子类的实例
  2. 不支持多继承

4. 拷贝继承

这种继承方式类似jquery的$.extend()方法,拷贝对象也是一种继承。

function Cat(name){  var animal = new Animal();  for(var p in animal){    Cat.prototype[p] = animal[p];  }  Cat.prototype.name = name || 'cat';}
// Test Codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); // falseconsole.log(cat instanceof Cat); // true

特点:

  1. 支持多继承

缺点:

  1. 效率较低,内存占用高(因为要拷贝父类的属性)
  2. 无法获取父类不可枚举的方法(不可枚举方法,不能使用for in 访问到)

5. 组合继承

核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用

function Cat(name){  Animal.call(this);  this.name = name || 'cat';}Cat.prototype = new Animal();
// Test Codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); // true

特点:

  1. 弥补了方式2的缺陷,可以继承实例属性/方法,也可以继承原型属性/方法
  2. 既是子类的实例,也是父类的实例
  3. 不存在引用属性共享问题
  4. 可传参
  5. 函数可复用

缺点:

  1. 调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)

6. 寄生组合继承

核心:通过寄生方式,砍掉父类的实例属性,这样,在调用两次父类的构造的时候,就不会初始化两次实例方法/属性,避免的组合继承的缺点

function Cat(name){  Animal.call(this);  this.name = name || 'Tom';}(function(){  // 创建一个没有实例方法的类  var Super = function(){};  Super.prototype = Animal.prototype;  //将实例作为子类的原型  Cat.prototype = new Super();})();
// Test Codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); // true

特点:

  1. 堪称完美

缺点:

  1. 实现较为复杂

7. ES6类的继承

ES6的Class之间可以通过extends关键字实现继承,这比ES5的通过修改原型链实现继承,要简单很多,这也是平常大多数面向对象语言的方式。

class Animal{  //构造函数    constructor(name){        this.name=name||'Animal';    }    speak(word){        console.log(this.name + ' is speaking: ' + word);    }    eat(food){        console(this.name + " is eating:" + food);    }}
class Cat extends Animal{    constructor(){        super();        this.name = 'cat';    }}
// Test Codevar cat = new Cat();console.log(cat.name); //catconsole.log(cat.speak("miao")); //cat is speaking: miaoconsole.log(cat.eat("fish")); //cat is eating:fishconsole.log(cat instanceof Animal); // trueconsole.log(cat instanceof Cat); // true

特点:

  1. class:ES6及ES6以上才有该方法。
  2. class的出现将原型继承简化了很多,class的目的就是让定义类更简单。
  3. extends来继承对象,中间的原型之类的就可以免去,就可以继承扩展class

参考:
MDN web docs
http://www.cnblogs.com/humin/p/4556820.html

原创粉丝点击