当前位置:首页 > 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'

组合继承

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

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'

寄生式继承

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

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关键字实现语法糖形式的继承,底层仍基于原型链机制。推荐在现代项目中使用。

js实现继承方式

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防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js实现视口

js实现视口

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

js实现 功能

js实现 功能

在 JavaScript 中实现功能通常涉及多个关键步骤,具体取决于功能需求。以下是常见功能的实现方法和示例: 基本功能实现 使用函数封装逻辑是最直接的方式。例如,实现一个计算两数之和的函数: f…

js实现搜索

js实现搜索

实现搜索功能的方法 在JavaScript中实现搜索功能可以通过多种方式完成,以下是几种常见的实现方法。 使用数组的filter方法 通过数组的filter方法可以筛选出符合条件的数据项。假设有一个…