当前位置:首页 > JavaScript

js继承如何实现

2026-03-01 00:27:27JavaScript

在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方法实现属性继承。

js继承如何实现

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

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

const child = new Child('child');
console.log(child.name); // 输出 'child'
child.sayName(); // 报错,无法继承原型方法

组合继承

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

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

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

寄生组合式继承

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

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

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

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

ES6 Class继承

使用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'

每种继承方式各有优缺点,选择时需要根据具体场景决定。原型链继承和构造函数继承存在明显缺陷,组合继承和寄生组合式继承更为常用。ES6的class语法是推荐的现代写法。

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

相关文章

js 实现倒计时

js 实现倒计时

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

js实现图片放大缩小

js实现图片放大缩小

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

如何实现vue

如何实现vue

安装 Vue.js 使用 npm 或 yarn 安装 Vue.js。确保已安装 Node.js 环境。 npm install vue # 或 yarn add vue 创建 Vue 实例 在 HT…

js实现报表

js实现报表

使用JavaScript实现报表 在JavaScript中实现报表功能可以通过多种方式完成,常见的方法包括使用原生JavaScript、第三方库(如Chart.js、D3.js)或结合后端数据渲染。以…

vue如何实现分离

vue如何实现分离

Vue 实现代码分离的方法 Vue 提供了多种方式实现代码分离,提升项目的可维护性和模块化程度。以下是常见的几种方法: 组件化开发 将功能拆分为独立的 Vue 组件,每个组件包含自己的模板、逻辑和样…

vue如何实现加减

vue如何实现加减

在 Vue 中实现加减功能 使用数据绑定和事件处理 通过 Vue 的数据绑定和事件处理机制可以轻松实现加减功能。定义一个数据变量存储数值,通过方法增减该变量。 <template>…