当前位置:首页 > 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中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选…

js实现乘

js实现乘

JavaScript 实现乘法运算 在 JavaScript 中实现乘法运算可以通过多种方式完成,包括基本运算符、函数封装以及高级算法(如大数乘法)。以下是几种常见方法: 使用基本乘法运算符 Ja…

vue如何实现

vue如何实现

Vue 实现方法 Vue 提供了多种方式来实现功能,具体取决于需求。以下是一些常见场景的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。在组件中可以通过 pr…

php如何实现直播

php如何实现直播

实现直播功能的方法 PHP可以通过结合其他技术和工具来实现直播功能。以下是几种常见的方法: 使用流媒体服务器 配置流媒体服务器如Nginx-RTMP、Red5或Wowza。这些服务器支持RTM…

js实现类

js实现类

JavaScript 实现类的几种方式 JavaScript 是一种基于原型的语言,但在 ES6 之后引入了 class 语法糖,使得实现类更加直观。以下是几种常见的实现方式: 使用构造函数 传统的…

js实现列表

js实现列表

使用 JavaScript 实现列表 JavaScript 提供了多种方式来实现列表功能,包括数组操作、DOM 元素动态生成等。以下是几种常见的实现方法: 使用数组存储列表数据 数组是 JavaS…