当前位置:首页 > JavaScript

js 实现 extends

2026-02-01 10:14:31JavaScript

实现 JavaScript 中的 extends 功能

在 JavaScript 中,extends 关键字用于实现类之间的继承。以下是几种实现继承的方式,包括 ES6 的 class 语法和传统的原型链继承方法。

使用 ES6 的 classextends

ES6 引入了 classextends 语法,使得继承更加直观和易于理解。

class Parent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Hello, ${this.name}`);
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name); // 调用父类的 constructor
    this.age = age;
  }

  greet() {
    super.greet(); // 调用父类的 greet 方法
    console.log(`I am ${this.age} years old`);
  }
}

const child = new Child('Alice', 10);
child.greet(); // 输出: Hello, Alice\nI am 10 years old

使用原型链继承(ES5)

在 ES5 中,可以通过原型链实现继承。

function Parent(name) {
  this.name = name;
}

Parent.prototype.greet = function() {
  console.log('Hello, ' + this.name);
};

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

// 设置原型链
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

// 添加子类方法
Child.prototype.greet = function() {
  Parent.prototype.greet.call(this);
  console.log('I am ' + this.age + ' years old');
};

var child = new Child('Alice', 10);
child.greet(); // 输出: Hello, Alice\nI am 10 years old

使用 Object.setPrototypeOf

另一种方式是使用 Object.setPrototypeOf 来设置原型链。

function Parent(name) {
  this.name = name;
}

Parent.prototype.greet = function() {
  console.log('Hello, ' + this.name);
};

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

// 设置原型链
Object.setPrototypeOf(Child.prototype, Parent.prototype);

// 添加子类方法
Child.prototype.greet = function() {
  Parent.prototype.greet.call(this);
  console.log('I am ' + this.age + ' years old');
};

var child = new Child('Alice', 10);
child.greet(); // 输出: Hello, Alice\nI am 10 years old

使用混合继承(组合继承)

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

function Parent(name) {
  this.name = name;
}

Parent.prototype.greet = function() {
  console.log('Hello, ' + this.name);
};

function Child(name, age) {
  Parent.call(this, name); // 继承属性
  this.age = age;
}

// 继承方法
Child.prototype = new Parent();
Child.prototype.constructor = Child;

// 添加子类方法
Child.prototype.greet = function() {
  Parent.prototype.greet.call(this);
  console.log('I am ' + this.age + ' years old');
};

var child = new Child('Alice', 10);
child.greet(); // 输出: Hello, Alice\nI am 10 years old

使用 extends 与自定义行为

可以通过覆盖 extends 的行为来实现更复杂的继承逻辑。

js 实现 extends

class CustomParent {
  constructor(name) {
    this.name = name;
  }

  greet() {
    console.log(`Custom Hello, ${this.name}`);
  }
}

class CustomChild extends CustomParent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }

  greet() {
    super.greet();
    console.log(`Custom I am ${this.age} years old`);
  }
}

const customChild = new CustomChild('Alice', 10);
customChild.greet(); // 输出: Custom Hello, Alice\nCustom I am 10 years old

以上方法提供了多种实现 JavaScript 继承的方式,可以根据具体需求选择适合的方式。

标签: jsextends
分享给朋友:

相关文章

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const random…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js实现的游戏

js实现的游戏

JavaScript 游戏开发基础 JavaScript 是开发网页游戏的流行选择,因其无需插件即可在浏览器中运行。以下是一些关键技术和资源: HTML5 Canvas Canvas 提供了绘制图形…

js实现排序

js实现排序

数组排序方法 JavaScript提供了内置的sort()方法用于数组排序。默认情况下,sort()将元素转换为字符串并按照Unicode码点排序。对于数字排序,需传入比较函数。 const num…

js实现弹框

js实现弹框

使用 alert 实现简单弹框 JavaScript 的 alert 方法可以直接显示一个简单的弹框,内容为纯文本: alert("这是一个简单的弹框"); 使用 confirm 实现确认弹框…