当前位置:首页 > JavaScript

怎样实现js的继承

2026-01-31 02:29:44JavaScript

在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();
Child.prototype.constructor = Child;

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',
  sayName: function() {
    console.log(this.name);
  }
};

const child = createChild(parent);
child.sayName(); // 输出 '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类继承

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

每种继承方式各有优缺点,可以根据实际需求选择合适的方法。ES6类继承是目前最推荐的方式,语法简洁且功能强大。

标签: js
分享给朋友:

相关文章

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

js实现复制

js实现复制

使用document.execCommand方法 在较旧的浏览器中,可以使用document.execCommand('copy')实现复制功能。创建一个临时的textarea或input元素,将需要…

js实现日历

js实现日历

实现日历的基本思路 使用JavaScript实现日历的核心是动态生成日期表格,并处理月份切换逻辑。需要计算当前月的天数、起始星期几,并动态渲染到页面上。 获取当前日期信息 通过Date对象获取当前…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClip…

js 实现链表

js 实现链表

链表的基本概念 链表是一种线性数据结构,由一系列节点组成,每个节点包含数据和指向下一个节点的指针。与数组不同,链表在内存中是非连续存储的,插入和删除操作效率较高。 链表的实现 在 JavaScri…

js实现二叉树

js实现二叉树

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