大网数据广告活动
Web 标准与前端开发

ReferenceError: must call super constructor before using 'this' in derived class constructor

· 52CE 我爱测

The JavaScript exception "must call super constructor before using 'this' in derived class constructor" occurs when the Operators/super is not called for a given derived class constructor, and the derived constructor tries to access the value of this, or the derived constructor h

The JavaScript exception "must call super constructor before using 'this' in derived class constructor" occurs when the Operators/super is not called for a given derived class constructor, and the derived constructor tries to access the value of this, or the derived constructor has already returned and the return value is not an object.

Message

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor (V8-based)
ReferenceError: must call super constructor before using 'this' in derived class constructor (Firefox)
ReferenceError: 'super()' must be called in derived constructor before accessing |this| or returning non-object. (Safari)

Error type

ReferenceError

What went wrong?

The super() call can only be called at most once for each new call to a derived class constructor. Often, you need to call it exactly once, because if you don't call it, the parent constructor cannot initialize the value of this, so you cannot access this in the derived constructor and the this is not considered a valid constructed object (and throws if the derived constructor completes in this state). The way around it is to return an object from the derived class constructor, in which case the object returned will be used as the constructed object instead of this, allowing you to not call super(). This is rarely done though.

Examples

Invalid cases

class Base {
  constructor() {
    this.x = 1;
  }
}

class Derived extends Base {
  constructor() {
    console.log(this.x);
    // The Base constructor is not called yet, so this.x is undefined
    // ReferenceError: must call super constructor before using 'this' in derived class constructor
  }
}

Valid cases

class Base {
  constructor() {
    this.x = 1;
  }
}

class Derived extends Base {
  constructor() {
    super();
    console.log(this.x); // 1
  }
}

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。