首页 > web前端 > js教程 > 正文

JavaScript如何实现继承_JavaScript原型链与class继承实现方法详解

雪夜
发布: 2025-11-09 17:51:02
原创
501人浏览过
JavaScript继承通过原型链和class实现,原型链继承共享引用导致问题,组合继承解决属性独立与方法复用,class语法更简洁但基于原型,寄生组合继承最优。

javascript如何实现继承_javascript原型链与class继承实现方法详解

JavaScript 实现继承的方式主要依赖于原型链和 ES6 引入的 class 语法。虽然 class 看起来像传统面向对象语言的类继承,但底层依然基于原型机制。下面详细说明两种方式的实现原理与使用方法。

原型链继承:通过 prototype 实现

JavaScript 中每个函数都有一个 prototype 属性,指向一个对象,该对象包含可以被实例共享的属性和方法。通过将子构造函数的原型指向父构造函数的实例,可以实现继承。

注意:这种方式无法向父类构造函数传参,且所有实例共享引用类型属性。

基本实现:

function Parent() {
  this.name = 'parent';
  this.colors = ['red', 'blue'];
}
Parent.prototype.getName = function() {
  return this.name;
};

function Child() {
  this.type = 'child';
}

// 继承父类
Child.prototype = new Parent();

const c1 = new Child();
c1.colors.push('green');
const c2 = new Child();
console.log(c2.colors); // ['red', 'blue', 'green'] —— 共享问题
登录后复制

这种写法的问题在于,所有 Child 实例共享同一个 Parent 实例,导致引用类型数据被共用。

立即学习Java免费学习笔记(深入)”;

组合继承:构造函数 + 原型链

为解决原型链继承的问题,常用“组合继承”:在子类构造函数中调用父类构造函数,并结合原型链。

实现方式:

function Parent(name) {
  this.name = name;
  this.colors = ['red', 'blue'];
}
Parent.prototype.getName = function() {
  return this.name;
};

function Child(name, age) {
  // 调用父类构造函数
  Parent.call(this, name);
  this.age = age;
}

// 建立原型链
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

const c1 = new Child('Tom', 12);
c1.colors.push('green');
const c2 = new Child('Jerry', 10);

console.log(c1.getName()); // 'Tom'
console.log(c2.colors);    // ['red', 'blue'] —— 不受影响
登录后复制

这样既保证了实例属性独立,又实现了方法复用,是较为推荐的传统继承方式。

ViiTor实时翻译
ViiTor实时翻译

AI实时多语言翻译专家!强大的语音识别、AR翻译功能。

ViiTor实时翻译 116
查看详情 ViiTor实时翻译

ES6 Class 继承:更清晰的语法糖

ES6 引入了 classextends 关键字,让继承写法更直观,但本质仍是基于原型。

使用示例:

class Parent {
  constructor(name) {
    this.name = name;
    this.colors = ['red', 'blue'];
  }
  getName() {
    return this.name;
  }
}

class Child extends Parent {
  constructor(name, age) {
    super(name); // 必须调用 super()
    this.age = age;
  }
  getAge() {
    return this.age;
  }
}

const c1 = new Child('Alice', 15);
c1.colors.push('yellow');
const c2 = new Child('Bob', 14);

console.log(c1.getName()); // 'Alice'
console.log(c2.colors);    // ['red', 'blue'] —— 独立
登录后复制

class 继承自动处理原型链连接,super() 调用父类构造函数,语法简洁且不易出错。

Object.create 与寄生组合继承

最优化的传统继承模式是“寄生组合继承”,它避免了组合继承中父类构造函数被调用两次的问题。

手动实现:

function inheritPrototype(Child, Parent) {
  const proto = Object.create(Parent.prototype);
  proto.constructor = Child;
  Child.prototype = proto;
}

function Parent(name) {
  this.name = name;
}

function Child(name, age) {
  Parent.call(this, name);
  this.age = age;
}

inheritPrototype(Child, Parent);

Child.prototype.getAge = function() {
  return this.age;
};
登录后复制

这种方式高效且语义清晰,是许多库内部实现继承的方法。

基本上就这些。理解原型链是掌握 JavaScript 继承的关键,而 class 则提供了更友好的封装。无论使用哪种方式,核心逻辑都围绕着原型查找机制展开。

以上就是JavaScript如何实现继承_JavaScript原型链与class继承实现方法详解的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号