语法错误:在严格模式代码中无法定义或分配 'arguments'/'eval'

当尝试创建名为 argumentsevalbinding 或分配给此类名称时,会发生 JavaScript 严格模式 特有的异常 "在严格模式代码中无法定义或分配 'arguments'" 或 "在严格模式代码中无法定义或分配 'eval'"。

¥The JavaScript strict mode-only exception "'arguments' can't be defined or assigned to in strict mode code" or "'eval' can't be defined or assigned to in strict mode code" occurs when attempting to create a binding called arguments or eval, or assign to such a name.

信息

¥Message

SyntaxError: Unexpected eval or arguments in strict mode (V8-based)
SyntaxError: 'arguments' can't be defined or assigned to in strict mode code (Firefox)
SyntaxError: Cannot modify 'arguments' in strict mode. (Safari)
SyntaxError: Cannot destructure to a parameter name 'arguments' in strict mode. (Safari)
SyntaxError: Cannot declare a variable named arguments in strict mode. (Safari)
SyntaxError: Cannot declare a catch variable named 'arguments' in strict mode. (Safari)
SyntaxError: 'arguments' is not a valid function name in strict mode. (Safari)

错误类型

¥Error type

SyntaxError

什么地方出了错?

¥What went wrong?

在严格模式下,名称 argumentseval 的行为就像它们是 保留字 一样:你不能让它们引用函数中的 arguments 对象或全局 eval 函数以外的任何东西。

¥In strict mode, the names arguments and eval behave as if they are reserved words: you cannot make they refer to anything other than the arguments object in functions or the global eval function.

示例

¥Examples

无效案例

¥Invalid cases

js
"use strict";

const arguments = [1, 2, 3];
console.log(Math.max(...arguments));

function foo(...arguments) {
  console.log(arguments);
}

有效案例

¥Valid cases

js
"use strict";

const args = [1, 2, 3];
console.log(Math.max(...args));

function foo(...args) {
  console.log(args);
}

也可以看看