JavaScript 六种常见的 继承方法

继承是面向对象编程中的一个重要概念,它允许一个对象(子类或派生类)获取另一个对象(父类或基类)的属性和方法。在 JavaScript 中,有多种方式可以实现继承。以下是六种常见的 JavaScript 继承方法以及详细介绍:

原型链继承(Prototype Inheritance):

这是 JavaScript 中最基本的继承方式。它通过将子类的原型对象指向父类的实例来实现继承。这意味着子类继承了父类的属性和方法,但也可能会有一些潜在的问题,比如所有子类的实例共享父类属性,导致意外的行为。

js 复制代码
function Parent() {
  this.name = 'Parent'
}

Parent.prototype.sayHello = function () {
  console.log(`Hello, I'm ${this.name}`)
}

function Child() {
  this.name = 'Child'
}

Child.prototype = new Parent()

const child = new Child()
child.sayHello() // 输出 "Hello, I'm Child"
构造函数继承(Constructor Inheritance):

这种继承方式通过在子类构造函数中调用父类构造函数来实现。这样可以避免原型链继承中的共享属性问题,但父类的方法不会被继承到子类的原型中。

js 复制代码
function Parent() {
this.name = 'Parent';
}

function Child() {
Parent.call(this);
this.name = 'Child';
}

const child = new Child();
console.log(child.name); // 输出 "Child"
组合继承(Combination Inheritance):

这是将原型链继承和构造函数继承结合的一种方式。它使用原型链继承来继承方法,使用构造函数继承来继承属性。但这种方法会调用两次父类构造函数,可能会导致性能问题。

js 复制代码
function Parent() {
this.name = 'Parent';
}

Parent.prototype.sayHello = function() {
console.log(`Hello, I'm ${this.name}`);
}

function Child() {
Parent.call(this);
this.name = 'Child';
}

Child.prototype = new Parent();

const child = new Child();
child.sayHello(); // 输出 "Hello, I'm Child"
原型式继承(Prototype-based Inheritance):

这种继承方式使用一个已有的对象作为模板来创建新对象,新对象可以继承模板对象的属性和方法。但需要注意的是,修改子对象的属性也会影响到模板对象。

js 复制代码
const parent = {
name: 'Parent',
sayHello: function() {
console.log(`Hello, I'm ${this.name}`);
}
};

const child = Object.create(parent);
child.name = 'Child';

child.sayHello(); // 输出 "Hello, I'm Child"
```js
寄生式继承(Parasitic Inheritance):

这种方式类似于原型式继承,但在创建新对象时可以添加额外的属性和方法,这些额外的属性和方法可以根据需要进行定制。

js 复制代码
function createChild(parent) {
const child = Object.create(parent);
child.name = 'Child';
child.sayHello = function() {
console.log(`Hello, I'm ${this.name}`);
};
return child;
}

const parent = {
name: 'Parent'
};

const child = createChild(parent);
child.sayHello(); // 输出 "Hello, I'm Child"
ES6 类继承(Class Inheritance):

ES6 引入了 class 关键字,使面向对象编程更加简洁。可以使用 extends 关键字实现类的继承,子类可以继承父类的属性和方法。

js 复制代码
class Parent {
constructor() {
this.name = 'Parent';
}

sayHello() {
console.log(`Hello, I'm ${this.name}`);
}
}

class Child extends Parent {
constructor() {
super();
this.name = 'Child';
}
}

const child = new Child();
child.sayHello(); // 输出 "Hello, I'm Child"

学习这些继承模式的关键在于理解它们的核心概念,否则当你在编写代码时遇到书本上的示例时,可能会感到为什么不直接采用更简单的继承方式呢?以原型式继承为例,它通过复制内部对象的一个副本来实现继承。这种方式不仅可以继承内部对象的属性,还能够自由调用其中包含的函数、对象,以及从源内部对象返回的内容。你可以添加属性,修改参数,从而定制化原型对象,而这些新增的属性不会相互影响。

相关推荐
To_OC8 小时前
别再串行写 await 了,Promise.all 才是并行请求的正确打开方式
前端·javascript·promise
To_OC9 小时前
LC 17 电话号码的字母组合:我的回溯算法,就是从这道题开窍的
javascript·算法·leetcode
你怎么知道我是队长11 小时前
JavaScript的变量和数据类型介绍
开发语言·javascript·ecmascript
名字还没想好☜12 小时前
Next.js 中间件实战:鉴权、重定向与 A/B 分流
开发语言·前端·javascript·中间件·react·next.js
广州灵眸科技有限公司12 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
心中有国也有家13 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
kaiking_g13 小时前
vue项目中,静态导入 vs 动态导入 详解
前端·javascript·vue.js
仙人球部落15 小时前
-python-LangGraph框架(3-31-LangGraph 「合并式状态管理」的原理与实践)
开发语言·javascript·python
sunfdf16 小时前
Next.js 新手从零部署到首跑实战指南
开发语言·javascript·ecmascript
SmartBoyW16 小时前
前端死磕:一文彻底搞懂 JS 事件循环 (Event Loop) 与宏微任务
前端·javascript