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

SyntaxError: use of super property/member accesses only valid within methods or eval code within methods

· 52CE 我爱测

The JavaScript exception "use of super property/member accesses only valid within methods or eval code within methods" occurs when the Operators/super or `super[x]` syntax is used outside of a method.

The JavaScript exception "use of super property/member accesses only valid within methods or eval code within methods" occurs when the Operators/super or super[x] syntax is used outside of a method.

Message

SyntaxError: 'super' keyword unexpected here (V8-based)
SyntaxError: use of super property accesses only valid within methods or eval code within methods (Firefox)
SyntaxError: super is not valid in this context. (Safari)

Error type

SyntaxError

What went wrong?

The super.x syntax is used to access properties on the prototype of the current object. It can be used in methods of both object literals and classes, field initializers, and static initialization blocks, but not in other contexts.

Examples

Invalid cases

You can't use super.x outside of a method in an object:

const obj = {
  __proto__: { x: 1 },
  x: super.x, // SyntaxError: use of super property accesses only valid within methods or eval code within methods
};

You can't use super.x in a function, even if that function has the effect of being a method:

function getX() {
  return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods
}

const obj = {
  getX,
  getX2: function () {
    return super.x; // SyntaxError: use of super property accesses only valid within methods or eval code within methods
  },
};

class Derived extends Base {
  getX = () => super.x;
}

Valid cases

You can use super.x in a method:

class Base {
  x = 1;
}

class Derived extends Base {
  getX() {
    return super.x;
  }
}

You can use super.x in a field initializer:

class Derived extends Base {
  x = super.x;
}

You can use super.x in object methods too:

const obj = {
  __proto__: { x: 1 },
  getX() {
    return super.x;
  },
};

See also

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

资料来源与适用范围

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

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

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