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

SyntaxError: arguments is not valid in fields

· 52CE 我爱测

The JavaScript exception "SyntaxError: arguments is not valid in fields" occurs when the `arguments` identifier is read in a class field initializer or in a static initialization block, outside of a non-arrow function.

The JavaScript exception "SyntaxError: arguments is not valid in fields" occurs when the arguments identifier is read in a class field initializer or in a static initialization block, outside of a non-arrow function.

Message

SyntaxError: 'arguments' is not allowed in class field initializer or static initialization block (V8-based)
SyntaxError: arguments is not valid in fields (Firefox)
SyntaxError: Unexpected identifier 'arguments'. Cannot reference 'arguments' in class field initializer. (Safari)

Error type

SyntaxError

What went wrong?

A class field initializer expression or a class static initialization block does not have arguments in its scope. Trying to access it is a syntax error.

  • This is true even if arguments is bound in a parent scope (such as when the class is nested in a non-arrow function).
  • A non-arrow function declared within this scope will still bind its own arguments and read it normally.

Examples

function makeOne() {
  class C {
    args = { ...arguments }; // SyntaxError: arguments is not valid in fields
  }
  return new C();
}
let CArgs;
class C {
  static {
    CArgs = arguments; // SyntaxError: arguments is not valid in fields
  }
}
class C {
  args = {};
  constructor() {
    this.args = arguments; // You can use arguments in constructors
  }
  myMethod() {
    this.args = arguments; // You can also use it in methods
  }
}
function makeOne() {
  const _arguments = arguments;
  class C {
    args = { ..._arguments }; // Only the identifier is forbidden
  }
  return new C();
}

See also

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

资料来源与适用范围

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

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

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