当前位置:首页 > JavaScript

js 实现 extends

2026-02-01 10:14:31JavaScript

实现 JavaScript 中的 extends 功能

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

使用 ES6 的 classextends

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

js 实现 extends

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 来设置原型链。

js 实现 extends

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 的行为来实现更复杂的继承逻辑。

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实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现打印

js实现打印

使用window.print()方法实现打印 在JavaScript中,可以通过调用window.print()方法直接触发浏览器的打印功能。这会打开系统的打印对话框,用户可以选择打印机和设置打印选项…

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获…

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

js实现dh

js实现dh

JavaScript 实现 DH(Diffie-Hellman)密钥交换 Diffie-Hellman(DH)密钥交换是一种安全协议,允许双方在不安全的通信信道上建立一个共享密钥。以下是如何在 Jav…