当前位置:首页 > JavaScript

继承实现js

2026-03-14 02:27:00JavaScript

继承的概念

继承是面向对象编程的核心概念之一,允许一个类(子类)继承另一个类(父类)的属性和方法,从而实现代码复用和层次化设计。

原型链继承

通过原型链实现继承,子类的原型指向父类的实例。这是 JavaScript 中最基本的继承方式。

function Parent() {
  this.name = 'Parent';
}

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

function Child() {
  this.childName = 'Child';
}

Child.prototype = new Parent(); // 继承实现

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

构造函数继承

在子类构造函数中调用父类构造函数,使用 callapply 方法实现属性继承。

继承实现js

function Parent(name) {
  this.name = name;
}

function Child(name, age) {
  Parent.call(this, name); // 继承属性
  this.age = age;
}

const child = new Child('Alice', 10);
console.log(child.name); // 输出 "Alice"

组合继承

结合原型链继承和构造函数继承,既能继承父类原型的方法,又能继承实例属性。

function Parent(name) {
  this.name = name;
}

Parent.prototype.sayName = function() {
  console.log(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('Bob', 12);
child.sayName(); // 输出 "Bob"

原型式继承

利用 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 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, age) {
  Parent.call(this, name);
  this.age = age;
}

inheritPrototype(Child, Parent);

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

ES6 类继承

使用 classextends 关键字实现继承,语法更简洁直观。

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

  sayName() {
    console.log(this.name);
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name); // 调用父类构造函数
    this.age = age;
  }
}

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

注意事项

  • 原型链继承会导致引用类型的属性被所有实例共享。
  • 构造函数继承无法继承父类原型上的方法。
  • 组合继承会调用两次父类构造函数,可能影响性能。
  • ES6 类继承是语法糖,底层仍基于原型链实现。

标签: js
分享给朋友:

相关文章

js实现全屏

js实现全屏

实现全屏的基本方法 使用JavaScript实现全屏功能主要依赖Element.requestFullscreen()方法。现代浏览器均支持此API,但不同浏览器可能需要添加前缀。 // 触发全屏…

js如何实现继承

js如何实现继承

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

js分组实现

js分组实现

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

js实现延迟

js实现延迟

实现延迟的方法 在JavaScript中,实现延迟操作有多种方式,以下是几种常见的方法: 使用setTimeout函数 setTimeout是JavaScript中最常用的延迟执行方法。它接受一个回…

js实现二叉树

js实现二叉树

二叉树的基本概念 二叉树是一种树形数据结构,每个节点最多有两个子节点,分别称为左子节点和右子节点。二叉树的常见操作包括插入、删除、遍历等。 二叉树的节点定义 在JavaScript中,二叉树的节点可…

js实现选题

js实现选题

实现选题功能的JavaScript方法 基础实现方案 使用数组存储选项,通过随机索引选取: const options = ['选项A', '选项B', '选项C', '选项D']; const r…