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

SyntaxError: super() is only valid in derived class constructors

· 52CE 我爱测

The JavaScript exception "super() is only valid in derived class constructors" occurs when the Operators/super call is used somewhere that's not the body of a constructor in a class with `extends` keyword.

The JavaScript exception "super() is only valid in derived class constructors" occurs when the Operators/super call is used somewhere that's not the body of a constructor in a class with extends keyword.

Message

SyntaxError: 'super' keyword unexpected here (V8-based)
SyntaxError: super() is only valid in derived class constructors (Firefox)
SyntaxError: super is not valid in this context. (Safari)

Error type

SyntaxError

What went wrong?

The super() call is used to invoke the base constructor of a derived class, so the base class can initialize the this object. Using it anywhere else doesn't make sense.

super() can also be defined in an arrow function that's nested within the constructor. However, it cannot be defined in any other kind of function.

Examples

Invalid cases

You cannot call super() if the class has no extends, because there's no base class to call:

class Base {
  constructor() {
    super();
  }
}

You cannot call super() in a class method, even if that method is called from the constructor:

class Base {}

class Derived extends Base {
  constructor() {
    this.init();
  }

  init() {
    super();
  }
}

You cannot call super() in a function, even if the function is used as a constructor:

function Base(x) {
  this.x = x;
}

function Derived() {
  super(1);
}

Object.setPrototypeOf(Derived.prototype, Base.prototype);
Object.setPrototypeOf(Derived, Base);

Valid cases

You can call super() before calling any other method in the constructor:

class Base {}

class Derived extends Base {
  constructor() {
    super();
    this.init();
  }

  init() {
    // …
  }
}

You can call super() in an arrow function that's nested within the constructor:

class Base {}

class Derived extends Base {
  constructor() {
    const init = () => {
      super();
    };

    init();
  }
}

See also

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

资料来源与适用范围

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

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

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