当前位置:首页 > JavaScript

怎样实现js的继承

2026-03-01 17:34:23JavaScript

原型链继承

通过将子类的原型指向父类的实例实现继承。子类实例通过原型链访问父类的属性和方法。

function Parent() {
  this.name = 'parent';
}
Parent.prototype.say = function() {
  console.log(this.name);
};

function Child() {}
Child.prototype = new Parent(); // 核心步骤
const child = new Child();
child.say(); // 输出'parent'

构造函数继承

在子类构造函数中调用父类构造函数,使用call/apply改变this指向。只能继承父类实例属性,无法继承原型方法。

function Parent(name) {
  this.name = name;
}
function Child(name) {
  Parent.call(this, name); // 核心步骤
}
const child = new Child('child');
console.log(child.name); // 输出'child'

组合继承

结合原型链和构造函数继承,既通过原型链继承方法,又通过构造函数继承实例属性。缺点是会调用两次父类构造函数。

怎样实现js的继承

function Parent(name) {
  this.name = name;
}
Parent.prototype.say = function() {
  console.log(this.name);
};

function Child(name) {
  Parent.call(this, name); // 第一次调用
}
Child.prototype = new Parent(); // 第二次调用
const child = new Child('child');
child.say(); // 输出'child'

原型式继承

基于现有对象创建新对象,使用Object.create()实现。适合不需要单独构造函数的场景。

const parent = {
  name: 'parent',
  say: function() {
    console.log(this.name);
  }
};
const child = Object.create(parent); // 核心步骤
child.name = 'child';
child.say(); // 输出'child'

寄生式继承

在原型式继承基础上增强对象,添加额外方法。适用于需要为对象添加特殊方法的场景。

怎样实现js的继承

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'

寄生组合式继承

目前最理想的继承方式,通过借用构造函数继承属性,通过原型链混成形式继承方法。只调用一次父类构造函数。

function inheritPrototype(child, parent) {
  const prototype = Object.create(parent.prototype); // 创建父类原型副本
  prototype.constructor = child; // 修正constructor
  child.prototype = prototype; // 赋值给子类原型
}

function Parent(name) {
  this.name = name;
}
Parent.prototype.say = function() {
  console.log(this.name);
};

function Child(name) {
  Parent.call(this, name);
}
inheritPrototype(Child, Parent); // 核心步骤
const child = new Child('child');
child.say(); // 输出'child'

ES6 Class继承

使用extends关键字实现类继承,语法更简洁直观。底层实现仍然是基于原型链。

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

class Child extends Parent { // 核心步骤
  constructor(name) {
    super(name); // 必须调用super
  }
}
const child = new Child('child');
child.say(); // 输出'child'

标签: js
分享给朋友:

相关文章

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js实现代码雨

js实现代码雨

实现代码雨效果 使用HTML5 Canvas和JavaScript可以轻松实现经典的代码雨效果。以下是完整的实现代码和说明: HTML结构 <!DOCTYPE html> <…

js 实现图片 放大

js 实现图片 放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性可以实现图片的平滑放大效果。结合 JavaScript 监听鼠标事件控制放大状态: con…

js 实现全选

js 实现全选

实现全选功能的方法 使用 JavaScript 实现全选功能通常需要操作复选框(checkbox)的状态。以下是几种常见的实现方式。 通过 DOM 操作实现全选 // 获取全选复选框和子复选框 co…