当前位置: 首页 > news >正文

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

ES5 实现继承的几种方式

1. 原型链继承(Prototype Chaining)

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. 构造函数继承(借用构造函数)

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. 组合继承(最常用方式)

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

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); // 第二次调用Parentthis.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(); // 10const child2 = new Child('Jerry', 8);
console.log(child2.colors); // ['red', 'blue'] (不共享)

优点

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

缺点

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

4. 原型式继承

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. 寄生式继承

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. 寄生组合式继承(最佳方式)

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

function inheritPrototype(child, parent) {const prototype = Object.create(parent.prototype); // 创建父类原型的副本prototype.constructor = child; // 修正constructorchild.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()
http://www.lqws.cn/news/163837.html

相关文章:

  • 微软认证考试科目众多?该如何选择?
  • 基于SpringBoot的房屋租赁系统的设计与实现(thymeleaf+MySQL)
  • IDEA202403 设置主题和护眼色
  • 【QT】qtdesigner中将控件提升为自定义控件后,css设置样式不生效(已解决,图文详情)
  • C# 委托UI控件更新例子,何时需要使用委托
  • Agentic AI 和 Agent AI 到底区别在哪里?
  • Redis大量key集中过期怎么办
  • Qt 开发中的父类与父对象的区别和父对象传递:如何选择 `QWidget` 或 `QObject`?
  • 基于Canvas实现画布
  • Spring Boot+Neo4j知识图谱实战:3步搭建智能关系网络!
  • 西门子SCL之IF-ELSIF语句详解及应用(安全控制代码)
  • 【计组】真题 2015 大题
  • Vue---vue使用AOS(滚动动画)库
  • 分布式光纤传感(DAS)技术应用解析:从原理到落地场景
  • 【QT】使用QT帮助手册找控件样式
  • 基于责任链模式进行订单参数的校验
  • 自动化办公集成工具:一站式解决文档处理难题
  • React Router 中 navigate 后浏览器返回按钮不起作用的问题记录
  • React Hooks 基础指南
  • 如果科技足够发达,是否还需要维持自然系统(例如生物多样性)中那种‘冗余’和‘多样性’,还是可以只保留最优解?
  • Python数据可视化科技图表绘制系列教程(四)
  • 动静态库的使用(Linux下)
  • 好得睐:以品质守味、以科技筑基,传递便捷与品质
  • Cilium动手实验室: 精通之旅---6.Cilium IPv6 Networking and Observability - Lab
  • 【2025年】解决Burpsuite抓不到https包的问题
  • 灵活控制,modbus tcp转ethernetip的 多功能水处理方案
  • 兰亭妙微 | 医疗软件的界面设计能有多专业?
  • 【QT】显示类控件
  • three.js中使用tween.js的chain实现动画依次执行
  • Python训练营打卡Day45