当前位置:首页 > 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
分享给朋友:

相关文章

js实现

js实现

JavaScript 实现方法 JavaScript 是一种广泛使用的编程语言,适用于网页开发、服务器端编程以及移动应用开发。以下是几种常见的 JavaScript 实现方法: 网页交互功能 使用…

js实现轮播

js实现轮播

实现基础轮播效果 使用HTML结构创建轮播容器和图片元素: <div class="carousel"> <div class="carousel-inner">…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 使用JavaScript实现拖拽功能需要监听鼠标事件,包括mousedown、mousemove和mouseup。以下是实现的基本逻辑: const draggableEleme…

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Paren…

js轮播图实现原理

js轮播图实现原理

轮播图的基本原理 轮播图通过动态切换显示的内容(图片、文本等)实现视觉上的滑动效果。核心原理是利用CSS和JavaScript控制元素的显示与隐藏,或通过变换位置实现滑动动画。 实现方法一:…