【JS进阶】ES5 实现继承的几种方式

ES5 实现继承的几种方式

1. 原型链继承(Prototype Chaining)

javascript 复制代码
function Parent(name) {
  this.name = name || 'Parent';
  this.colors = ['red', 'blue'];
}

Parent.prototype.sayName = function() {
  console.log(this.name);
};

function Child() {}

// 关键:将子类的原型指向父类的实例
Child.prototype = new Parent();

const child1 = new Child();
child1.sayName(); // "Parent"

问题

  • 父类实例属性成为子类原型属性,会被所有子类实例共享
  • 无法向父类构造函数传参

2. 构造函数继承(借用构造函数)

javascript 复制代码
function Parent(name) {
  this.name = name || 'Parent';
  this.colors = ['red', 'blue'];
}

function Child(name) {
  // 关键:在子类构造函数中调用父类构造函数
  Parent.call(this, name);
}

const child1 = new Child('Child1');
console.log(child1.name); // "Child1"

优点

  • 可以向父类传参
  • 避免了引用属性共享问题

缺点

  • 无法继承父类原型上的方法
  • 方法都在构造函数中定义,无法复用

3. 组合继承(最常用方式)

结合原型链继承和构造函数继承:

javascript 复制代码
function Parent(name) {
  this.name = name;
  this.colors = ['red', 'blue'];
}

Parent.prototype.sayName = function() {
  console.log(this.name);
};

function Child(name, age) {
  // 继承实例属性
  Parent.call(this, name); // 第二次调用Parent
  this.age = age;
}

// 继承原型方法
Child.prototype = new Parent(); // 第一次调用Parent
Child.prototype.constructor = Child; // 修正constructor
Child.prototype.sayAge = function() {
  console.log(this.age);
};

const child1 = new Child('Tom', 10);
child1.colors.push('green');
console.log(child1.colors); // ['red', 'blue', 'green']
child1.sayName(); // "Tom"
child1.sayAge(); // 10

const child2 = new Child('Jerry', 8);
console.log(child2.colors); // ['red', 'blue'] (不共享)

优点

  • 实例属性独立
  • 可以继承原型方法
  • 可以向父类传参

缺点

  • 调用了两次父类构造函数

4. 原型式继承

javascript 复制代码
function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

const parent = {
  name: 'Parent',
  colors: ['red', 'blue']
};

const child1 = object(parent);
child1.name = 'Child1';
child1.colors.push('green');

const child2 = object(parent);
console.log(child2.colors); // ['red', 'blue', 'green'] (共享引用属性)

类似于 Object.create(),ES5 中新增了 Object.create() 方法实现相同功能。

5. 寄生式继承

javascript 复制代码
function createAnother(original) {
  const clone = Object.create(original); // 创建新对象
  clone.sayHi = function() { // 增强对象
    console.log('Hi');
  };
  return clone;
}

const parent = {
  name: 'Parent'
};

const child = createAnother(parent);
child.sayHi(); // "Hi"

6. 寄生组合式继承(最佳方式)

解决组合继承调用两次父类构造函数的问题:

javascript 复制代码
function inheritPrototype(child, parent) {
  const prototype = Object.create(parent.prototype); // 创建父类原型的副本
  prototype.constructor = child; // 修正constructor
  child.prototype = prototype; // 赋值给子类原型
}

function Parent(name) {
  this.name = name;
  this.colors = ['red', 'blue'];
}

Parent.prototype.sayName = function() {
  console.log(this.name);
};

function Child(name, age) {
  Parent.call(this, name); // 只调用一次Parent构造函数
  this.age = age;
}

inheritPrototype(Child, Parent); // 实现原型继承

Child.prototype.sayAge = function() {
  console.log(this.age);
};

const child1 = new Child('Tom', 10);
child1.sayName(); // "Tom"
child1.sayAge(); // 10

优点

  • 只调用一次父类构造函数
  • 原型链保持不变
  • 能够正常使用 instanceofisPrototypeOf()
相关推荐
逆向新手1 分钟前
js逆向-某省特种设备aes加密研究
javascript·爬虫·python·逆向·js
光影少年2 分钟前
react navite相比较传统开发有啥优势?
前端·react.js·前端框架
岁月宁静3 分钟前
软件开发工程师如何借助 AI 工具进行软件自测
前端·ai编程·测试
秦苒&6 分钟前
【C语言】详解数据类型和变量(一):数据类型介绍、 signed和unsigned、数据类型的取值范围、变量、强制类型转换
c语言·开发语言·c++·c#
我爱学习_zwj6 分钟前
动态HTTP服务器实战:解析请求与Mock数据
开发语言·前端·javascript
NMBG227 分钟前
外卖综合项目
java·前端·spring boot
小白阿龙8 分钟前
样式不生效/被覆盖(CSS优先级陷阱)
前端·css
flashlight_hi10 分钟前
LeetCode 分类刷题:110. 平衡二叉树
javascript·算法·leetcode
Beginner x_u11 分钟前
Vue 事件机制全面解析:原生事件、自定义事件与 DOM 冒泡完全讲透
前端·javascript·vue.js·dom
Emma_Maria11 分钟前
关于vant-ui-vue 的datepicker 时间选择错乱问题的处理
前端·vue.js·ui