当前位置:首页 > JavaScript

js实现继承方式

2026-01-30 10:18:43JavaScript

原型链继承

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

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'

组合继承

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

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(); // 第二次调用
Child.prototype.constructor = Child;

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

原型式继承

基于已有对象创建新对象,类似Object.create()的模拟实现。适合不需要单独构造函数的场景,但存在引用属性共享问题。

function createObj(o) {
  function F() {}
  F.prototype = o;
  return new F();
}

const parent = { name: 'parent' };
const child = createObj(parent);
console.log(child.name); // 输出 'parent'

寄生式继承

在原型式继承基础上增强对象,添加额外方法。适用于主要关注对象增强而非类型创建的场景。

js实现继承方式

function createAnother(o) {
  const clone = Object.create(o);
  clone.say = function() {
    console.log(this.name);
  };
  return clone;
}

const parent = { name: 'parent' };
const child = createAnother(parent);
child.say(); // 输出 '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 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);
  }
}

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

标签: 方式js
分享给朋友:

相关文章

js 实现vue

js 实现vue

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

jquery.js

jquery.js

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

js实现全屏

js实现全屏

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

js实现图片放大缩小

js实现图片放大缩小

实现图片放大缩小的基础方法 使用CSS的transform: scale()属性可以快速实现图片的放大缩小效果。通过JavaScript动态修改该属性值: const img = document.…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…