Summary
The set
syntax binds an object property to a function to be called when there is an attempt to set that property.
Syntax
{set prop(val) { . . . }}
Parameters
-
prop
- the name of the property to bind to the given function
-
val
-
an alias for the variable that holds the value attempted to be assigned to
prop
Description
In JavaScript, a setter can be used to execute a function whenever a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.
Note the following when working with the set
syntax:
- It can have an identifier which is either a number or a string;
- It must have exactly one parameter (see Incompatible ES5 change: literal getter and setter functions must now have exactly zero or one arguments for more information);
- It must not appear in an object literal with another
set
or with a data entry for the same property.
({ set x(v) { }, set x(v) { } }
and{ x: ..., set x(v) { } }
are forbidden )
A setter can be removed using the delete
operator.
Examples
Defining a setter on new objects in object initializers
This will define a pseudo-property current
of object o
that, when assigned a value, will update log
with that value:
var o = { set current (str) { return this.log[this.log.length] = str; }, log: [] }
Note that current
is not defined and any attempts to access it will result in undefined
.
Defining a setter on existing objects using defineProperty
To append a setter to an existing object later at any time, use {{jsxref("Object.defineProperty()")}}.
var o = { a:0 }; Object.defineProperty(o, "b", { set: function (x) { this.a = x / 2; } }); o.b = 10; // Runs the setter, which assigns 10 / 2 (5) to the 'a' property console.log(o.a) // 5
Removing a setter with the delete
operator
delete o.current;
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('ES5.1', '#sec-11.1.5', 'Object Initializer')}} | {{Spec2('ES5.1')}} | Initial definition. |
{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}} | {{Spec2('ES6')}} |
Browser compatibility
Based on Robert Nyman's page. No support (notably in IE6-8) means that the script will trigger a syntax error.
{{ CompatibilityTable() }}
Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{ CompatGeckoDesktop("1.8.1") }} | 1 | 9 | 9.5 | 3 |
Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} | {{ CompatUnknown() }} |
SpiderMonkey-specific notes
- Starting with JavaScript 1.8.1, setters are no longer called when setting properties in object and array initializers.
See also
- getter
- {{jsxref("Operators/delete", "delete")}}
- {{jsxref("Object.defineProperty()")}}
- {{jsxref("Object.defineGetter", "__defineGetter__")}}
- {{jsxref("Object.defineSetter", "__defineSetter__")}}
- Defining Getters and Setters in JavaScript Guide