coderef

Accessor methodsLimited availability

The __defineGetter__() and __defineSetter__() methods of objects bind a function to a property, which is called on setting or reading the property.

Not yet supported in all major browsers — check the table and provide a fallback.

Discouraged: TC39 included accessor methods in Annex B of the ECMAScript specification, which covers JavaScript features with "one or more undesirable characteristics and in the absence of legacy usage would be removed."

Browser support

DesktopChrome
1+Supportedsince Dec 11, 2008
Current: 155
Edge
12+Supportedsince Jul 29, 2015
Current: 154
Firefox
1+Supportedsince Nov 9, 2004
Current: 157
Safari
1–2Not supported
3+Supportedsince Oct 26, 2007
Current: 27
MobileChrome Android
18+Supportedsince Jun 27, 2012
Current: 155
Firefox Android
4+Supportedsince Mar 29, 2011
Current: 157
Safari iOS
1+Supportedsince Jun 29, 2007
Current: 27

How to use

Support is still inconsistent across browsers. Use feature detection and test carefully.

Example

Full syntax and examples are on MDN: defineGetter .

Feature detection

JS · check before using
if ("defineGetter" in Object || ("prototype" in Object && "defineGetter" in Object.prototype)) {
  // Object.defineGetter is available
}

Sub-features

Support for each part of the feature (4). The overall table above shows when the whole feature works.

Sub-featureChromeEdgeFirefoxSafariChrome AndroidFirefox AndroidSafari iOS
Object.defineGetter112131841
Object.defineSetter112131841
Object.lookupGetter112131841
Object.lookupSetter112131841