extends 仅建立继承关系,不调用父类构造器;super() 才负责调用父类构造器,且必须在子类 constructor 中首行显式调用,否则报错。

extends 的作用是建立继承链,不是执行初始化
写 class Child extends Parent 只表示 Child 在原型链上继承 Parent 的方法和静态成员,也意味着 Child 的 prototype.__proto__ 指向 Parent.prototype,同时 Child.__proto__ 指向 Parent。但它不会自动运行父类构造函数。
super() 才是调用父类构造器的关键
子类如果有自定义 constructor,就必须第一行调用 super(...args),把参数传给父类构造器,完成 this 的初始化:
- 不调用 super() → 报错
ReferenceError: Must call super constructor in derived class before accessing 'this' - super() 必须在使用 this 前执行,否则语法报错
- super() 内部会以子类实例为 this,执行父类 constructor,从而赋予 x、y、name 等初始属性
常见写法示例
假设父类定义为:
class Point {<br> constructor(x, y) {<br> this.x = x;<br> this.y = y;<br> }<br>}
子类正确调用方式:
class ColorPoint extends Point {<br> constructor(x, y, color) {<br> super(x, y); // ✅ 这里才真正调用了父类构造器<br> this.color = color;<br> }<br>}
如果省略 super(x, y),或把它放在 this.color = color 后面,都会导致运行时错误。
没有自定义 constructor 时的隐式行为
如果子类没写 constructor,JavaScript 会自动补上一个默认构造器:
constructor(...args) {<br> super(...args);<br>}
也就是说,父类构造器仍会被调用,只是你没看见而已。


















