当前位置:首页 > JavaScript

js继承如何实现的

2026-01-30 09:50:25JavaScript

继承的实现方式

JavaScript 中的继承主要通过原型链实现,以下是几种常见的继承方式:

原型链继承

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

function Parent() {
  this.name = 'parent';
}
Parent.prototype.getName = function() {
  return this.name;
};

function Child() {}
Child.prototype = new Parent();

const child = new Child();
console.log(child.getName()); // 'parent'

构造函数继承

在子类构造函数中调用父类构造函数:

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.getName = function() {
  return this.name;
};

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}
Child.prototype = new Parent();
Child.prototype.constructor = Child;

const child = new Child('child', 10);
console.log(child.getName()); // 'child'

原型式继承

基于已有对象创建新对象:

const parent = {
  name: 'parent',
  getName: function() {
    return this.name;
  }
};

const child = Object.create(parent);
child.name = 'child';
console.log(child.getName()); // 'child'

寄生式继承

在原型式继承基础上增强对象:

function createChild(parent) {
  const child = Object.create(parent);
  child.sayHello = function() {
    console.log('Hello');
  };
  return child;
}

const parent = { name: 'parent' };
const child = createChild(parent);
child.sayHello(); // 'Hello'

寄生组合式继承

最理想的继承方式:

function inheritPrototype(child, parent) {
  const prototype = Object.create(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

function Parent(name) {
  this.name = name;
}
Parent.prototype.getName = function() {
  return this.name;
};

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}
inheritPrototype(Child, Parent);

const child = new Child('child', 10);
console.log(child.getName()); // 'child'

ES6类继承

使用class和extends语法:

class Parent {
  constructor(name) {
    this.name = name;
  }
  getName() {
    return this.name;
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name);
    this.age = age;
  }
}

const child = new Child('child', 10);
console.log(child.getName()); // 'child'

每种继承方式都有其适用场景和优缺点,实际开发中应根据需求选择合适的继承方式。ES6类继承是目前最简洁和推荐的方式。

js继承如何实现的

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

相关文章

js实现

js实现

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

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterv…

jquery.js

jquery.js

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

css3结合js制作

css3结合js制作

CSS3 结合 JavaScript 制作动态效果 CSS3 和 JavaScript 结合可以实现丰富的动态效果,包括动画、交互和响应式设计。以下是几种常见的实现方式: CSS3 动画与 Jav…

vue手写签名如何实现

vue手写签名如何实现

实现 Vue 手写签名的步骤 使用 canvas 实现基础签名功能 在 Vue 项目中创建一个组件,利用 HTML5 的 canvas 元素实现手写签名功能。通过监听鼠标或触摸事件来捕获用户的绘制路径…

vue如何实现曲线图

vue如何实现曲线图

使用 ECharts 实现曲线图 在 Vue 项目中安装 ECharts 依赖: npm install echarts --save 引入 ECharts 并创建基础图表组件: <te…