当前位置:首页 > 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'

构造函数继承

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

js继承如何实现的

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'

原型式继承

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

js继承如何实现的

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实现

实现 JavaScript 功能的方法 在 JavaScript 中实现特定功能通常涉及多个步骤。以下是常见的实现方法: 基础语法和变量声明 使用 let 或 const 声明变量: let co…

vue如何实现

vue如何实现

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

js实现复制到剪贴板

js实现复制到剪贴板

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

vue如何实现排序

vue如何实现排序

实现数组排序 在Vue中可以通过计算属性或方法对数组进行排序。使用JavaScript的sort()方法结合Vue的响应式特性实现动态排序。 data() { return { item…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…

js实现 拖动

js实现 拖动

实现拖动的步骤 HTML 结构 创建一个可拖动的元素和一个放置区域: <div id="draggable" draggable="true">拖动我</div> <d…