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

JavaScript 中的继承

在网课中,老师着重讲解了原型继承,而在javaScript里面还有其他几种不同的继承方式,我们今天在这里拓展一下

1. 原型继承

这是最基本的继承方式,通过让子类的原型对象指向父类的实例来实现继承。

这种方法的缺点:所有子类实例共享同一个父类实例的属性,创建子类实例时无法向父类构造函数传参

​
​
function Person() {this.eye = 2this.head = 1}function Woman() {}Woman.prototype = new Person()Woman.prototype.constructor = WomanWoman.prototype.baby = function () {console.log('baby');}const womans = new Woman()console.log(womans);function Man() {}Man.prototype = new Person()Man.prototype.constructor = Manconst mans = new Man()console.log(mans);​​

2. 构造函数继承(经典继承)

通过在子类构造函数中调用父类构造函数来实现继承。

优点:可以向父类构造函数传递参数,避免了引用类型属性被所有实例共享

缺点:无法继承父类原型上的方法和属性

 function Person() {this.eye = 2this.head = 1}function Woman() {}Woman.prototype = new Person()Woman.prototype.constructor = WomanWoman.prototype.baby = function () {console.log('baby');}const womans = new Woman()console.log(womans);function Man() {}Man.prototype = new Person()Man.prototype.constructor = Manconst mans = new Man()console.log(mans);

3. 组合继承(最常用)

结合原型链继承和构造函数继承的优点。

优点:可以继承父类实例属性和原型属性;可以向父类构造函数传递参数;每个新实例引入的构造函数属性是私有的

缺点:调用了两次父类构造函数

function Parent(name) {this.name = name;
}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;const child = new Child('Tom', 10);
child.sayName(); // "Tom"

4. 原型式继承

基于已有对象创建新对象

function createObj(o) {function F() {}F.prototype = o;return new F();
}const parent = {name: 'Parent',sayName: function() {console.log(this.name);}
};const child = createObj(parent);
child.name = 'Child';
child.sayName(); // "Child"

5. ES6 类继承

ES6 引入了 class 语法糖,使继承更加清晰。ES6 的类继承实际上是语法糖,底层仍然是基于原型链的实现。

class Parent {constructor(name) {this.name = name;}sayName() {console.log(this.name);}
}class Child extends Parent {constructor(name, age) {super(name); // 调用父类的constructorthis.age = age;}sayAge() {console.log(this.age);}
}const child = new Child('Tom', 10);
child.sayName(); // "Tom"
child.sayAge(); // 10

http://www.dtcms.com/a/288080.html

相关文章:

  • MySQL——约束类型
  • 【RK3576】【Android14】分区划分
  • Java行为型模式---中介者模式
  • HOT100——排序篇Leetcode215. 数组中的第K个最大元素
  • 深度解析 rag-vector-agent-semantic-kernel:基于 Semantic Kernel 的 Agentic RAG 实践
  • 变频器实习Day10
  • JS原型相关知识
  • EINO框架解读:字节跳动开源的大模型应用开发框架
  • 【jquery详细讲解】
  • Vue Swiper组件
  • Vue组件化开发小案例
  • 在开发板tmp目录下传输文件很快的原因和注意事项:重启开发板会清空tmp文件夹,记得复制文件到其他地方命令如下(cp 文件所在路径 文件要复制到的路径—)
  • GitLab 社区版 10.8.4 安装、汉化与使用教程
  • GPU集群如何规划
  • 子串算法题
  • Web攻防-身份验证篇JWT令牌空密钥未签名密钥爆破JWKJWUKID算法替换CVE报告复盘
  • 在Vscode中使用Kimi K2模型:实践指南,三分钟生成个小游戏
  • TypeScript 中的「类」:从语法到实战的完整指南
  • 论C/C++的条件编译#if、#ifdef、#ifndef、#undef
  • Promise入门
  • 三级知识点汇总(详解)【c++】——2
  • 我用Cursor,1周上线了一个虚拟资料流量主小程序技术选型
  • Linux“一切皆文件“设计哲学 与 Linux文件抽象层:struct file与file_operations的架构解析
  • 【ChatOpenAI】常用方法详解
  • HOT100——动态规划篇Leetcode221. 最大正方形
  • C++ std::thread线程类 相关问题、函数总结
  • 单调队列深度解析(下)
  • 如何解决 ‘NoneType‘ object has no attribute ‘get‘问题
  • GA-BP遗传算法优化BP神经网络数据生成,采用SVM分类模型评估
  • LM317 芯片推荐电路中保护二极管工作原理