语法错误:setter 函数必须有一个参数
当声明 setter 并且参数列表不完全由一个形式参数组成时,会发生 JavaScript 异常 "setter 函数必须有一个参数"。
¥The JavaScript exception "setter functions must have one argument" occurs when a setter is declared and the parameter list is not consisted of exactly one formal parameter.
信息
¥Message
SyntaxError: Setter must have exactly one formal parameter. (V8-based) SyntaxError: Setter function argument must not be a rest parameter (V8-based) SyntaxError: setter functions must have one argument (Firefox) SyntaxError: Unexpected token ','. setter functions must have one parameter. (Safari) SyntaxError: Unexpected token '...'. Expected a parameter pattern or a ')' in parameter list. (Safari)
错误类型
什么地方出了错?
¥What went wrong?
set
属性语法看起来像一个函数,但它更严格,并且并非所有函数语法都允许。setter 始终只使用一个参数调用,因此使用任何其他数量的参数定义它都可能是错误的。此参数可以是 destructured 或具有 默认值,但不能是 剩余参数。
¥The set
property syntax looks like a function, but it is stricter and not all function syntax is allowed. A setter is always invoked with exactly one argument, so defining it with any other number of parameters is likely an error. This parameter can be destructured or have a default value, but it cannot be a rest parameter.
请注意,此错误仅适用于使用 set
语法的属性设置器。如果你使用 Object.defineProperty()
等定义 setter,则 setter 被定义为普通函数,尽管如果 setter 需要任何其他数量的参数,它仍可能是一个错误,因为它将只使用一个参数被调用。
¥Note that this error only applies to property setters using the set
syntax. If you define the setter using Object.defineProperty()
, etc., the setter is defined as a normal function, although it's likely still an error if the setter expects any other number of arguments, as it will be called with exactly one.