The set syntax binds an object property to a function to be called when there is an attempt to set that property.
{set prop(val) { . . . }}
{set [expression](val) { . . . }} propvalprop.
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:
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.
This will define a pseudo-property current of object language that, when assigned a value, will update log with that value:
var language = {
set current(name) {
this.log.push(name);
},
log: []
}
language.current = 'EN';
console.log(language.log); // ['EN']
language.current = 'FA';
console.log(language.log); // ['EN', 'FA']
Note that current is not defined and any attempts to access it will result in undefined.
delete operatorIf you want to remove the setter, you can just delete it:
delete o.current;
defineProperty
To append a setter to an existing object later at any time, use 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 var expr = 'foo';
var obj = {
baz: 'bar',
set [expr](v) { this.baz = v; }
};
console.log(obj.baz); // "bar"
obj.foo = 'baz'; // run the setter
console.log(obj.baz); // "baz"
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) The definition of 'Object Initializer' in that specification. | Standard | Initial definition. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Method definitions' in that specification. | Standard | Added computed property names. |
| ECMAScript Latest Draft (ECMA-262) The definition of 'Method definitions' in that specification. | Draft |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 1 | Yes | 2 | 9 | 9.5 | 3 |
| Computed property names | 46 | Yes | 34 | No | 47 | No |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | 1 | 18 | Yes | 4 | Yes | Yes | Yes |
| Computed property names | 46 | 46 | Yes | 34 | Yes | No | 5.0 |
| Server | |
|---|---|
| Node.js | |
| Basic support | Yes |
| Computed property names | Yes |
deleteObject.defineProperty()__defineGetter____defineSetter__
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set