当前位置:首页 > JavaScript

实现继承js

2026-04-03 22:15:56JavaScript

实现继承的方式

在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方法改变this指向。

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'

组合继承

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

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'

原型式继承

通过一个函数创建一个临时构造函数,将其原型指向传入的对象,返回该构造函数的实例。

function createObject(obj) {
  function F() {}
  F.prototype = obj;
  return new F();
}
const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};
const child = createObject(parent);
child.sayName(); // 输出 'parent'

寄生式继承

在原型式继承的基础上增强对象,返回一个经过扩展的对象。

function createAnother(obj) {
  const clone = Object.create(obj);
  clone.sayHello = function() {
    console.log('hello');
  };
  return clone;
}
const parent = {
  name: 'parent'
};
const child = createAnother(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.sayName = function() {
  console.log(this.name);
};

function Child(name) {
  Parent.call(this, name);
}
inheritPrototype(Child, Parent);
const child = new Child('child');
child.sayName(); // 输出 'child'

ES6 Class继承

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

实现继承js

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'

每种继承方式各有优缺点,选择适合场景的方式实现继承。

标签: js
分享给朋友:

相关文章

js实现全选

js实现全选

实现全选功能的方法 在JavaScript中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选框…

js实现变形

js实现变形

使用 CSS 和 JavaScript 实现元素变形 在 JavaScript 中实现变形效果通常结合 CSS 的 transform 属性,通过动态修改该属性实现元素的旋转、缩放、倾斜或平移。 基…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…

js实现显示隐藏

js实现显示隐藏

显示隐藏的实现方法 在JavaScript中,实现元素的显示和隐藏可以通过多种方式完成。以下是几种常见的方法: 修改CSS的display属性 通过改变元素的display属性可以在none(隐藏)…

js计算器的实现

js计算器的实现

实现基础结构 HTML部分需要创建计算器的基本框架,包含显示区域和按钮。使用<div>容器包裹整个计算器,内部设置一个显示屏和多个按钮。 <div class="calculato…