Accessor methods
The __defineGetter__() and __defineSetter__() methods of objects bind a function to a property, which is called on setting or reading the property.
Overview
The __defineGetter__() and __defineSetter__() methods of objects bind a function to a property, which is called on setting or reading the property.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
javascript.builtins.Object.defineGetter Deprecated | 1 | 12 | 1 | 3 | 18 | 1 |
| Built-in object | ||||||
Object.defineSetter Deprecated The _defineSetter_() method of Object instances binds an object's property to a function to be called when an attempt is made to set that property. | 1 | 12 | 1 | 3 | 18 | 1 |
Object.lookupGetter Deprecated The _lookupGetter_() method of Object instances returns the function bound as a getter to the specified property. | 1 | 12 | 1 | 3 | 18 | 1 |
Object.lookupSetter Deprecated The _lookupSetter_() method of Object instances returns the function bound as a setter to the specified property. | 1 | 12 | 1 | 3 | 18 | 1 |
- Starting with Firefox 48, this method can no longer be called at the global scope without any object. A `TypeError` will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.
- Starting with Firefox 48, this method can no longer be called at the global scope without any object. A `TypeError` will be thrown otherwise. Previously, the global object was used in these cases automatically, but this is no longer the case.
Syntax
const user = {
firstName: 'Taro',
lastName: 'Yamada',
get fullName() {
return `${this.lastName} ${this.firstName}`;
},
set fullName(value) {
[this.lastName, this.firstName] = value.split(' ');
}
};
user.fullName; // 'Taro Yamada'
user.fullName = 'Hanako Tanaka'; Live demo
fulne-mu. autosync
and name by 々 to write and, getter to fulne-mu that automaticallyupdate..
Inputvalue. valid-tion
Setter Use, not-positive value(negative. number etc) that sett...
tax-includedprice. autocalculation
bodyprice sett and, (10%) includeprice automaticallycalculation..
Use cases
Using Accessor methods
The __defineGetter__() and __defineSetter__() methods of objects bind a function to a property, which is called on setting or reading the property.
Cautions
- Limited browser support. Check compatibility before use.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features