当前位置:首页 > JavaScript

js实现继承原理

2026-01-30 11:43:15JavaScript

原型链继承

通过将子类的原型对象指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法,但所有子类实例共享父类实例的属性,可能导致修改污染。

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'

构造函数继承

在子类构造函数中调用父类构造函数,使用callapply改变this指向。可以解决原型链继承的属性共享问题,但无法继承父类原型上的方法。

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

function Child(name) {
  Parent.call(this, name); // 构造函数继承
}

const child = new Child('child');
console.log(child.name); // 输出 'child'

组合继承

结合原型链继承和构造函数继承,既继承父类实例属性又继承原型方法。但会调用两次父类构造函数,导致子类原型上存在冗余属性。

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'

寄生式继承

在原型式继承基础上增强对象,添加额外方法。与工厂模式类似,无法实现函数复用。

function createChild(parent) {
  const child = Object.create(parent);
  child.newMethod = function() {
    console.log('new method');
  };
  return child;
}

寄生组合式继承

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

function inheritPrototype(child, parent) {
  const prototype = Object.create(parent.prototype);
  prototype.constructor = child;
  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关键字实现语法糖继承,底层仍基于原型链。支持super调用父类构造函数和方法。

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

class Child extends Parent {
  constructor(name) {
    super(name);
  }
}

const child = new Child('child');
child.say(); // 输出 'child'

js实现继承原理

标签: 原理js
分享给朋友:

相关文章

jquery.js

jquery.js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,用于简化 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它的设计宗旨是“Write Less, Do Mor…

vue实现js休眠

vue实现js休眠

Vue 中实现 JavaScript 休眠 在 Vue 中实现 JavaScript 休眠通常需要使用异步方式,以避免阻塞主线程。以下是几种常见方法: 使用 setTimeout 和 Promis…

css动画制作原理

css动画制作原理

CSS动画制作原理 CSS动画通过关键帧(@keyframes)和动画属性(如animation-name、animation-duration)实现动态效果。核心原理分为两部分:定义动画关键帧和绑定…

vue的基本实现原理

vue的基本实现原理

Vue 的基本实现原理 Vue.js 的核心实现原理主要围绕响应式系统、虚拟 DOM 和模板编译展开。以下是其关键实现机制的详细说明: 响应式系统 Vue 使用 Object.definePro…

vue实现放大镜原理

vue实现放大镜原理

Vue 实现放大镜原理 实现放大镜效果的核心原理是通过鼠标移动事件获取位置信息,动态计算放大区域并显示放大后的图像。以下是基于 Vue 的实现方法: 基本结构设计 HTML 部分需包含原图容器、放…

js实现动画

js实现动画

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