SyntaxError: setter functions must have one argument
The JavaScript exception "setter functions must have one argument" occurs when asetter 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)
Error type
What went wrong?
Theset
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 bedestructured or have adefault value, but it cannot be arest parameter.
Note that this error only applies to property setters using theset
syntax. If you define the setter usingObject.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.
Examples
Invalid cases
const obj = { set value() { this._value = Math.random(); },};
Valid cases
// You must declare one parameter, even if you don't use itconst obj = { set value(_ignored) { this._value = Math.random(); },};// You can also declare a normal method insteadconst obj = { setValue() { this._value = Math.random(); },};