当前位置:首页 > JavaScript

js实现继承怎么实现

2026-04-06 15:31:28JavaScript

原型链继承

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

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

function Child() {}
Child.prototype = new Parent(); // 核心步骤

const child = new Child();
child.sayName(); // 输出 "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.sayName = function() {
  console.log(this.name);
};

function Child(name) {
  Parent.call(this, name); // 继承属性
}
Child.prototype = new Parent(); // 继承方法

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

缺点:父类构造函数被调用两次(Parent.callnew Parent()),性能损耗。

原型式继承

基于已有对象创建新对象,通过 Object.create() 实现。

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

适用场景:不需要构造函数的简单对象继承。
缺点:引用类型属性仍会被共享。

寄生式继承

在原型式继承基础上增强对象,添加额外方法或属性。

function createChild(parent) {
  const child = Object.create(parent);
  child.newMethod = function() {
    console.log('New method');
  };
  return child;
}
const parent = { name: 'Parent' };
const child = createChild(parent);
child.newMethod(); // 输出 "New method"

缺点:与原型式继承类似,无法复用方法。

寄生组合式继承

最优解决方案,通过 Object.create() 避免重复调用父类构造函数。

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

function Child(name) {
  Parent.call(this, name); // 继承属性
}
Child.prototype = Object.create(Parent.prototype); // 继承方法
Child.prototype.constructor = Child; // 修复构造函数指向

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

优点:只调用一次父类构造函数,避免原型链冗余,保持原型链完整性。

ES6 Class 继承

使用 extendssuper 语法糖实现继承,本质是寄生组合式继承的语法封装。

js实现继承怎么实现

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

class Child extends Parent {
  constructor(name) {
    super(name); // 调用父类构造函数
  }
}

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

优势:语法简洁,符合现代 JavaScript 标准。

标签: js
分享给朋友:

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现动画

js实现动画

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

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

链表实现js

链表实现js

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和一个指向下一个节点的指针(单向链表)或两个指针(双向链表)。与数组不同,链表在内存中非连续存储,插入和删除操作更高效,但随机…

js实现视口

js实现视口

js实现视口检测的方法 使用JavaScript检测元素是否进入视口(viewport)可以通过Intersection Observer API或手动计算元素位置实现。以下是两种常见方法: Int…

js实现跑马灯

js实现跑马灯

实现跑马灯效果 使用HTML和JavaScript可以轻松实现跑马灯效果。以下是两种常见的实现方式: HTML结构 <div id="marquee"> <span>…