es6的class是语法糖,本质仍是构造函数和原型链操作;它通过更清晰的语法封装了es5中手动操作原型链的过程,未引入新继承模型。

ES6 的 class 不是新机制,它没有引入任何新的继承模型,只是把原本需要手动操作原型链的繁琐逻辑,用更清晰、更结构化的语法包装起来——所以它被准确地称为“语法糖”。
class 本质就是构造函数
你写一个 class Person { constructor(name) { this.name = name; } },Babel 编译后实际生成的是:
- 一个普通函数:
function Person(name) { this.name = name; } - 自动插入
_classCallCheck检查,确保必须用new调用,否则报错 - 所有实例方法(如
sayHello())都被挂到Person.prototype上,和 ES5 手写一模一样
extends 和 prototype 链完全对应
当你写 class Child extends Parent,底层只做三件事,全是原型链操作:
-
Child.prototype = Object.create(Parent.prototype):让子类原型链上接父类原型 -
Child.prototype.constructor = Child:修复构造器引用,避免丢失 -
Child.__proto__ = Parent:使静态方法也能被继承(比如Parent.method()→Child.method())
super() 是对父构造函数的显式调用
子类构造函数中必须第一行写 super(),它不是魔法,而是等价于:
-
Parent.call(this, ...args):执行父类初始化逻辑,给this添加父类属性 - 不调用就访问
this会直接报错,因为子类this必须由父类构造函数先“激活” - 在普通方法里写
super.xxx(),本质是Parent.prototype.xxx.call(this)
instanceof 和原型链完全一致
一个 new Child() 实例的原型链仍是:
child.__proto__ === Child.prototypeChild.prototype.__proto__ === Parent.prototypeParent.prototype.__proto__ === Object.prototype
因此 child instanceof Child 和 child instanceof Parent 都为 true,验证了 class 继承没改变 JS 底层行为,只是让写法更稳、更可读。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南











