es6的class本质是构造函数的语法糖,仍基于原型链实现继承;编译后转为带new检查的函数,方法挂载到prototype,extends和super均对应底层原型操作,完全兼容instanceof。

ES6 的 class 不是新机制,而是对原型继承的封装——它让写法更清晰,但运行时仍靠函数和原型链干活。
class 本质仍是构造函数
用 Babel 编译后,class Person 会被转成普通函数:function Person(name) { this.name = name; }
同时自动加入 _classCallCheck 检查,确保必须用 new 调用,否则报错“Cannot call a class as a function”。
类中定义的方法(如 sayHello())会被挂到 Person.prototype 上,和 ES5 手写原型一模一样。
extends 并非魔法,只是自动设置原型链
当你写 class Child extends Parent,底层实际做了三件事:
- 调用
Object.create(Parent.prototype),让Child.prototype原型指向Parent.prototype - 把
Child.prototype.constructor重新指回Child,避免丢失构造器 - 将
Child.__proto__指向Parent,使静态方法也能被继承
super() 是强制初始化 this 的关键
子类构造函数中必须第一行调用 super(),否则访问 this 会报错。它的作用是:
- 执行父类构造函数,把父类的实例属性(如
this.name)赋给当前this - 确保
this已被正确绑定,后续才能添加子类自己的属性(如this.id) - 在方法中用
super.xxx(),等价于Parent.prototype.xxx.call(this)
继承关系可验证,且完全兼容 instanceof
实例的原型链没变:一个 new Child() 对象,其 __proto__ 链是:
因此以下全部为 true:
child instanceof Childchild instanceof Parentchild instanceof Object
这说明 class 继承没有破坏 JavaScript 的底层一致性,只是让开发者少写易错的原型操作。











