当前位置:首页 > 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 方法实现属性继承。

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() 方法创建一个新对象,以现有对象作为原型。

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 关键字实现继承,语法更简洁直观。

继承实现js

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
分享给朋友:

相关文章

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…

js jquery

js jquery

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够…

js实现自举

js实现自举

实现 JavaScript 自举的方法 JavaScript 自举(Bootstrap)通常指通过 JavaScript 动态加载自身或依赖的脚本,实现运行时环境的初始化或扩展。以下是几种常见实现方式…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…

js实现下拉菜单

js实现下拉菜单

使用HTML和CSS创建基础结构 HTML部分需要包含一个触发下拉的按钮和隐藏的下拉菜单内容: <div class="dropdown"> <button class="dr…

js实现菜单

js实现菜单

实现基本HTML结构 使用HTML创建菜单的基本框架,通常包含<ul>和<li>元素。示例结构如下: <ul id="menu"> <li><…