JavaScript实现继承[总结篇]💯

1,669 阅读6分钟

哈喽,大家好!我是奶茶不加糖。一个喜欢喝奶茶的前端攻城狮
( 哈哈,今天又是摸鱼的一天(* ̄︶ ̄) )😁😁😁

前言

🙌 最近在学ts的过程中又复习了一遍es5里面的继承方式,相信继承也是很多面试官喜欢问的知识点,特别有笔试题的总是要我们写一些继承方法哈哈哈😄,这里就跟大家一起来复习和巩固一下叭叭叭💪💪💪

JS 继承的实现方式

既然是要实现继承,当然需要一个父亲了,不然继承啥是不哈哈哈
父类如下:

// 定义一个动物类
function Animal (name) {
  // 属性
  this.name = name || 'Animal';
  // 实例方法
  this.sleep = function(){
    console.log(this.name + '正在睡觉!');
  }
  //实例引用属性
  this.features = [];
}
// 原型方法
Animal.prototype.eat = function() {
  console.log(this.name + '正在吃!');
};

1、构造函数实现继承 (又叫对象冒充实现继承)

核心:这里使用的原理就是在Cat里面,把Animalthis指向改为是Catthis指向,从而实现继承
重点:用.call().apply()将父类构造函数引入子类函数(在子类函数中做了父类函数的自执行(复制))

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}

// Test Code
var cat = new Cat();
console.log(cat.name); //Tom
//instanceof 判断元素是否在另一个元素的原型链上
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
cat.sleep() //Tom正在睡觉!
cat.eat() //会报错cat.eat is not a function
对象冒充可以继承构造函数里面的属性和方法,没法继承原型链上的属性和方法

❌缺点:

  • 实例并不是父类的实例,只是子类的实例
  • 只能继承父类的实例属性和方法,不能继承父类原型上的属性/方法
  • 无法实现函数复用,每个子类都有父类实例函数的副本,影响性能
    (每个子类都有父类函数的属性和方法的副本,当cat调用Animal上的方法时,Animal内部的this指向的是catAnimal内部的this上的属性和方法都被复制到了cat上面,如果每个子类的实例都复制一遍父类的属性和方法,就会占用很大的内存,而且当父类的方法发生改变了时,已经创建好的子类实例并不能更新方法,因为已经复制了原来的父类方法当成自己的方法了)

2、原型链实现继承

核心: 将父类的实例作为子类的原型 这里把 Cat 的原型改为是 Animal 的实例,从而实现继承
重点:让新实例的原型等于父类的实例。

function Cat(){
}
Cat.prototype.play = function() {
  console.log(this.name + '正在玩!');
};
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';

// Test Code
var cat = new Cat('zhangsan'); //缺点4传参也没效果
var cat1 = new Cat('lisi');
cat.name = 'Tom';
cat.features.push('red');
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true
cat.play() //会报错cat.play is not a function 缺点1 所以把play方法移动到Cat.prototype = new Animal()的后面
cat.eat() //cat正在吃!  解决了构造函数实现继承的缺点2 <(* ̄▽ ̄*)/
cat.sleep() //cat正在睡觉!
//针对父类实例值类型成员的更改,不影响
console.log(cat.name); // "Tom"
console.log(cat1.name); // "cat"
//针对父类实例引用类型成员的更改,会通过影响其他子类实例  缺点2
console.log(cat.features); // ['red']
console.log(cat1.features); // ['red']

❌缺点:

  • 如果要为子类新增属性或者方法,只能在new Animal() 之后,并不能放在构造函数中,如上的代码示例,如果新增的方法放在改变子类原型的指向之前,改变指向之后新增的方法自然就没用了,子类的prototype已经指向了父类了
  • 子类的所有实例,共用所有的父类属性,子类不能拥有自己的属性,如果有多个实例时,其中一个实例修改了父类引用类型的值,那么所有的实例都会发生改变,例如我只想其中的一个实例的features数组改为['red'],那么所有的实例该方法都会发生改变
  • 不能多继承,因为是改变了原型链的指向,不能指向多个父类,因此只能单继承
  • 创建子类时,无法向父类构造函数传参,因为在改变子类的原型链指向之后,子类的属性和方法是无效的

3、组合继承(组合原型链继承和借用构造函数继承)

核心:通过调用父类构造,继承父类的属性并保留传参的优点,然后通过将父类实例作为子类原型,实现函数复用
重点:结合了以上两种模式的优点,传参和复用

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
Cat.prototype = new Animal(); //还有另一种写法 Cat.prototype = Animal.prototype;

// 组合继承也是需要修复构造函数指向的。
Cat.prototype.constructor = Cat;

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true

❌缺点:
这种方式调用了两次父类的构造函数,生成了两份实例,相同的属性既存在于实例中也存在于原型中

4、拷贝继承

function Cat(name){
  var animal = new Animal();
  for(var p in animal){
    Cat.prototype[p] = animal[p];
  }
  this.name = name || 'Tom';
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true

❌缺点:

  • 无法获取父类不可枚举的方法,这种方法是用for in 来遍历Animal中的属性,例如多选框的checked属性,这种就是不可枚举的属性
  • 效率很低,内存占用高

5、寄生组合继承(推荐)

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

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
(function(){
  // 创建一个没有实例方法的类
  var Super = function(){};
  Super.prototype = Animal.prototype;
  //将实例作为子类的原型
  Cat.prototype = new Super();
})();

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true

后语

希望看到这里朋友可以动动手点个赞👍哦,你们的支持就是对我最大的鼓励💪💪💪!!!