当前位置: 首页 > 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()

相关文章:

  • python项目如何创建docker环境
  • OpenCV 图像像素的逻辑操作
  • React Hooks 指南:何时使用 useEffect ?
  • OPenCV CUDA模块目标检测----- HOG 特征提取和目标检测类cv::cuda::HOG
  • 阿里云域名怎么绑定
  • 概述侧边导航的作用与价值
  • 结合Jenkins、Docker和Kubernetes等主流工具,部署Spring Boot自动化实战指南
  • 06.最长连续序列
  • (头歌作业)-6.5 幻方(project)
  • Web后端基础(Maven基础)
  • 8.axios Http网络请求库(1)
  • 源码编译 Cas Server 4/5/6/7
  • 无人机军用与民用技术对比分析
  • App使用webview套壳引入h5(三)——解决打包为app后在安卓机可物理返回但是在苹果手机无法测滑返回的问题
  • Uniapp 二维码生成与解析完整教程
  • win32相关(远程线程和远程线程注入)
  • Spring框架学习day7--SpringWeb学习(概念与搭建配置)
  • 深度解析地质灾害风险普查:RS与GIS技术在泥石流、滑坡灾害中的应用,ArcGIS数据管理、空间数据转换、专题地图制作、DEM分析及实战案例分析
  • 实用对比图软件推荐:快速呈现信息差异
  • opencv-4.8.1到 sln
  • 可信网站身份验证 必须做吗/百度搜图入口
  • 电商网站定制/什么是竞价推广
  • 网站建立的意义/全网推广方案
  • 做网站需要什么技术员/百度推广工具有哪些
  • 创新的福州网站建设/网站有哪些平台
  • 电商网络营销是干什么的/免费seo公司