当前位置:首页 > JavaScript

继承实现js

2026-02-01 04:11:02JavaScript

继承的概念

继承是面向对象编程的核心概念之一,允许子类继承父类的属性和方法,同时可以扩展或重写父类的功能。JavaScript 主要通过原型链实现继承。

原型链继承

通过将子类的原型对象指向父类的实例实现继承。

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"

缺点:无法继承父类原型上的方法。

继承实现js

组合继承

结合原型链继承和构造函数继承,既继承属性又继承方法。

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"

缺点:父类构造函数被调用两次,效率较低。

原型式继承

基于现有对象创建新对象,使用 Object.create()

继承实现js

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.sayHi = function() {
    console.log('Hi');
  };
  return child;
}

const parent = { name: 'Parent' };
const child = createChild(parent);
child.sayHi(); // 输出 "Hi"

寄生组合式继承

最优解决方案,避免组合继承中父类构造函数被调用两次的问题。

function inheritPrototype(Child, Parent) {
  const prototype = Object.create(Parent.prototype); // 创建父类原型的副本
  prototype.constructor = Child; // 修正构造函数指向
  Child.prototype = prototype; // 赋值给子类原型
}

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

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

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

ES6 的类继承

使用 classextends 关键字简化继承实现。

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"

优势:语法简洁,底层仍基于原型链实现。

标签: js
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swipe…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js实现跑马灯

js实现跑马灯

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

js实现显示隐藏

js实现显示隐藏

显示隐藏的实现方法 在JavaScript中,实现元素的显示和隐藏可以通过多种方式完成。以下是几种常见的方法: 修改CSS的display属性 通过改变元素的display属性可以在none(隐藏)…

js分页实现

js分页实现

分页的基本原理 分页的核心是通过计算当前页码和数据偏移量,从服务器或本地数据中截取对应范围的数据进行展示。通常需要以下参数:当前页码(currentPage)、每页条数(pageSize)、总数据量(…