当前位置:首页 > JavaScript

实现继承js

2026-02-28 21:49:12JavaScript

原型链继承

通过将子类的原型对象指向父类的实例来实现继承。子类实例通过原型链访问父类的属性和方法。

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"

构造函数继承

在子类构造函数中调用父类构造函数,使用call或apply方法改变this指向。

function Parent(name) {
  this.name = name;
}
function Child(name) {
  Parent.call(this, name); // 继承实现
}

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

组合继承

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

实现继承js

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(); // 继承方法

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

原型式继承

基于已有对象创建新对象,使用Object.create()方法实现。

const parent = {
  name: 'parent',
  sayName: function() {
    console.log(this.name);
  }
};

const child = Object.create(parent); // 继承实现
child.name = 'child';
child.sayName(); // 输出 "child"

寄生式继承

在原型式继承基础上增强对象,添加额外方法。

实现继承js

function createAnother(original) {
  const clone = Object.create(original);
  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, age) {
  Parent.call(this, name);
  this.age = age;
}
inheritPrototype(Child, Parent); // 继承实现

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

ES6类继承

使用class和extends关键字实现更清晰的继承语法。

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('child', 10);
child.sayName(); // 输出 "child"

标签: js
分享给朋友:

相关文章

js实现分页

js实现分页

实现分页的基本思路 分页功能通常需要处理数据分割、页码生成和用户交互。核心逻辑包括计算总页数、根据当前页截取数据、渲染页码按钮等。 前端分页实现(静态数据) 假设已有全部数据,仅需前端分页展示:…

js实现验证码

js实现验证码

使用Canvas生成图形验证码 在HTML中创建一个Canvas元素用于绘制验证码。通过JavaScript随机生成数字或字母组合,并添加干扰线、噪点等干扰元素增强安全性。 <canvas i…

js轮播图实现原理

js轮播图实现原理

轮播图的基本原理 轮播图通过动态切换显示的内容(图片、文本等)实现视觉上的滑动效果。核心原理是利用CSS和JavaScript控制元素的显示与隐藏,或通过变换位置实现滑动动画。 实现方法一:CSS…

js实现延迟

js实现延迟

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

js 实现链表

js 实现链表

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

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…