es6 class继承是原型链继承的语法糖,本质未变:①设child.prototype.__proto__指向parent.prototype;②重置constructor;③设child.__proto__指向parent;④super()必须首行调用以初始化this。

ES6 的 class 继承不是新机制,而是对原型链继承的封装和规范。它没有改变 JavaScript 面向对象的本质,只是把原本需要手动操作、容易出错的原型链设置,变成自动完成的语法糖。
class 继承干了哪些事?
写 class Child extends Parent 时,引擎底层实际做了三件关键事:
- 让
Child.prototype.__proto__指向Parent.prototype,使实例能沿原型链访问父类方法 - 把
Child.prototype.constructor重设为Child,避免构造器丢失 - 将
Child.__proto__指向Parent,让静态方法也能被子类继承
super() 不是可选,而是必须
子类构造函数中,super() 必须出现在第一行,否则无法访问 this。它的作用很明确:
- 执行父类构造函数,把
this.name、this.age等实例属性挂到当前this上 - 确保
this已被正确初始化,后续才能添加子类自己的属性(如this.id) - 在普通方法中写
super.speak(),等价于Parent.prototype.speak.call(this)
原型链没变,instanceof 依然可靠
一个 new Child() 实例,其原型链仍是:
Child.prototype → Parent.prototype → Object.prototype
所以以下判断全部成立:
-
child instanceof Child—— 是子类实例 -
child instanceof Parent—— 也是父类实例 -
child instanceof Object—— 仍是顶层对象
这说明 class 继承完全兼容 JavaScript 原有的运行时行为,没有破坏底层一致性。
对比 ES5 手动继承:少写什么,多得什么?
ES5 中实现同样继承,需手动处理四件事:
- 用
Parent.call(this, ...)继承实例属性 - 用
Object.create(Parent.prototype)设置原型链 - 手动修复
Child.prototype.constructor - 若要继承静态方法,还得额外写
Child.__proto__ = Parent
而 extends + super() 把这些都收束成两句清晰语法,既减少出错可能,也提升可读性。本质上还是原型链,只是更稳、更直、更少魔幻操作。











