语法错误:在严格模式代码中无法定义或赋值给“arguments”/“eval”
在严格模式下,当尝试创建一个名为 arguments
或 eval
的 绑定,或为这样的名称赋值时,会发生严格模式下唯一的异常 "'arguments' cannot be defined or assigned to in strict mode code" 或 "'eval' cannot be defined or assigned to in strict mode code"。
消息
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)
错误类型
问题出在哪里?
示例
无效情况
js
"use strict";
const arguments = [1, 2, 3];
console.log(Math.max(...arguments));
function foo(...arguments) {
console.log(arguments);
}
有效情况
js
"use strict";
const args = [1, 2, 3];
console.log(Math.max(...args));
function foo(...args) {
console.log(args);
}