banner
banner
banner
NEWS LETTER

7种强大的JavaScript继承技术

Scroll down

结论先行:JS 的继承写法一共七种,本质都是围绕两件事做取舍——实例属性能不能各自独立、父级方法能不能复用。只看结论:原型链继承和原型继承复用性最好但引用类型会被共享;构造函数继承属性独立但方法无法复用;组合继承两边兼顾却会调用两次父构造函数;寄生组合继承和 ES6 class extends 是当前的主流写法。日常开发直接用 class,理解原理时按下面七种依次过一遍。

背景:为什么会有七种写法

原型链提供方法复用,构造函数提供独立属性,两者怎么拼,就拼出了不同的继承方案。每种写法都是在“属性独立”和“方法复用”之间换位置,所以看每节的优点与缺点,比看代码更能记住差别。

1. 原型链继承

让子类原型指向一个父类实例,从而通过原型链拿到父类的属性和方法。

  • 最直接的继承方法之一
  • 允许对象通过原型连接的“链”继承属性和方法
1
2
3
4
5
6
7
8
9
10
11
12
function Animal() {
this.species = 'Mammal';
this.habits = ['sleep', 'eat'];
}
function Dog() {
this.breed = 'Bulldog';
}
Dog.prototype = new Animal();

let d1 = new Dog();
let d2 = new Dog();
d1.habits.push('bark'); // 向Animal.habits添加了bark

运行上面的代码后,d1.habits 与 d2.habits 指向同一个数组,因此 d2.habits 里也会出现 'bark'。

  • 优点:设置简单,为子类提供所有父原型方法的访问权限
  • 缺点:共享引用类型可能会导致副作用,为每个实例调用父对象的构造函数

2. 构造函数继承

在子构造函数里调用父构造函数,把父级的实例属性复制到子实例上。

  • 子对象通过在子构造函数中调用父对象的构造函数来继承属性
1
2
3
4
5
6
7
8
9
function Animal(species) {
this.species = species;
this.activities = [];
}
function Dog(breed) {
Animal.call(this, 'Mammal'); // 给Dog中的父species赋值
this.breed = breed;
}
let d = new Dog('dog')
  • 优点:每个实例都有不同的属性,参数可以传递给父级
  • 缺点:无法跨实例重用方法,从而导致冗余

3. 复合继承(组合继承)

把前两种拼起来:属性用 call 复制一份,方法挂在原型上复用。

  • 合并了原型链和构造函数继承,以便子类提供唯一属性和对父方法的访问
1
2
3
4
5
6
7
8
9
10
11
12
13
function Animal(species) {
this.species = species;
this.activities = [];
}
Animal.prototype.getSpecies = function() { // 父方法
return this.species;
};
function Dog(breed) {
Animal.call(this, 'Mammal');
this.breed = breed;
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
  • 优点:子类有自己的属性和原型方法
  • 缺点:父构造函数被调用两次,增加了开销

4. 寄生继承

在创建对象的同时对它做增强,再把这个对象返回,写法灵活但结构更绕。

  • 创建、修改和返回对象,使继承变得灵活,但增加了复杂性
1
2
3
4
5
6
7
8
9
10
function Animal() {
this.species = 'Mammal';
this.habits = ['eat', 'sleep'];
}
function Dog() {
Animal.call(this);
this.breed = 'Bulldog';
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
  • 优点:允许在克隆对象上使用原型扩展
  • 缺点:不太适合大型继承结构

5. 原型继承

用 Object.create() 直接指定新对象的原型,连构造函数都不需要。

  • Object.create() 直接创建具有指定原型的对象
1
2
3
4
5
6
7
8
9
10
11
12
let animal = {
species: 'Mammal',
traits: ['warm-blooded', 'vertebrate']
};
function createClone(obj) {
let clone = Object.create(obj);
clone.getTraits = function() {
return this.traits;
};
return clone;
}
let dog = createClone(animal);
  • 优点:简单且不需要构造函数
  • 缺点:所有实例共享 prototype 属性

6. 寄生组合继承

组合继承的改良版:不再 new Animal(),而是用 Object.create(Animal.prototype) 复制一份原型,避免多余的一次父构造函数调用。

  • 改进的复合继承,使用 Object.create()
1
2
3
4
5
6
7
8
9
10
function Animal(species) {
this.species = species;
this.traits = [];
}
function Dog(breed) {
Animal.call(this, 'Mammal');
this.breed = breed;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
  • 优点:解决了父构造函数被调用两次的问题,同时保留了组合继承的好处
  • 缺点:代码复杂性和需要额外的封装

7. ES6 类继承

class 与 extends 是原型继承的语法糖,底层仍是构造函数加原型链。

  • ES6 中,在 JS 里引入了 class 语法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Animal {
constructor(species) {
this.species = species;
}
getSpecies() {
return this.species;
}
}

class Dog extends Animal {
constructor(breed) {
super('Mammal');
this.breed = breed;
}
}
  • 优点:干净、可读的语法,类似于传统的 OOP
  • 缺点:可能需要转译,与基于原型的方法相比,性能略有不同

踩坑与注意点

  • 原型链上的引用类型永远共享:第 1 种和第 5 种里的 habits、traits 都是数组,任何一个实例改动它,其他实例都会跟着变。这是这两个方案最实际的坑。
  • 组合继承的 new Animal() 不带参数:此时原型上的 species 是 undefined,实例上的值来自 Animal.call(this, 'Mammal'),所以不影响结果,但多执行了一次父构造函数。
  • 用 ES6 类时子类构造器必须先 **super()**:在 super() 之前访问 this 会直接抛错,这是类继承与函数式继承最明显的差异。
其他文章
cover
Solid
  • 25/03/19
  • 10:06
  • JS框架
目录导航 置顶
  1. 1. 背景:为什么会有七种写法
  2. 2. 1. 原型链继承
  3. 3. 2. 构造函数继承
  4. 4. 3. 复合继承(组合继承)
  5. 5. 4. 寄生继承
  6. 6. 5. 原型继承
  7. 7. 6. 寄生组合继承
  8. 8. 7. ES6 类继承
  9. 9. 踩坑与注意点
请输入关键词进行搜索