当前位置:首页 > JavaScript

js继承如何实现

2026-01-30 09:23:34JavaScript

原型链继承

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

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

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

const c = new Child();
c.say(); // 输出"parent"

构造函数继承

在子类构造函数中调用父类构造函数,使用call/apply改变this指向。可以避免引用属性共享问题,但无法继承父类原型上的方法。

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

function Child(name) {
  Parent.call(this, name); // 核心步骤
}

const c = new Child('child');
console.log(c.name); // "child"
c.say(); // 报错,无法继承原型方法

组合继承

结合原型链和构造函数继承的方式。通过构造函数继承实例属性,通过原型链继承原型方法,但会调用两次父类构造函数。

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 c = new Child('child');
c.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 createAnother(original) {
  const clone = Object.create(original);
  clone.newMethod = function() {
    console.log('new method');
  };
  return clone;
}

const parent = { name: 'parent' };
const child = createAnother(parent);

寄生组合式继承

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

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 c = new Child('child');
c.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);
  }
}

const c = new Child('child');
c.say(); // "child"

js继承如何实现

标签: 如何实现js
分享给朋友:

相关文章

vue如何实现登录

vue如何实现登录

实现登录功能的基本步骤 使用Vue实现登录功能通常需要结合后端API、状态管理以及路由控制。以下是常见的实现方式: 创建登录表单组件 在Vue组件中构建包含用户名和密码输入框的表单: <…

vue如何实现mvvm

vue如何实现mvvm

Vue 实现 MVVM 的核心机制 Vue 的 MVVM(Model-View-ViewModel)实现依赖于数据绑定和响应式系统,通过以下核心机制完成: 数据劫持(响应式系统) Vue 使用 Ob…

vue如何实现单选

vue如何实现单选

Vue 实现单选的方法 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的实现方法: 使用 v-model 绑定单选按钮 通过 v-model 绑定到同一个变量,确保同一时间只有一个…

vue如何实现id

vue如何实现id

在 Vue 中实现 ID 绑定 Vue 提供了多种方式为元素或组件绑定唯一的 ID,可以根据具体需求选择合适的方法。 方法一:使用 v-bind 绑定静态或动态 ID <template…

Vue如何实现多级域名

Vue如何实现多级域名

Vue 实现多级域名的方法 Vue 本身是一个前端框架,多级域名的实现通常需要结合后端配置和前端路由。以下是几种常见的方法: 使用路由动态匹配 在 Vue Router 中可以通过动态路由参数…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…