This feature is deprecated in favor of defining getters using the object initializer syntax or the Object.defineProperty()
API. While this feature is widely implemented, it is only described in the ECMAScript specification because of legacy usage. This method should not be used since better alternatives exist.
The __defineGetter__
method binds an object's property to a function to be called when that property is looked up.
Syntax
obj.__defineGetter__(prop, func)
Parameters
prop
- A string containing the name of the property to bind to the given function.
func
- A function to be bound to a lookup of the specified property.
Return value
Description
The __defineGetter__
allows a getter to be defined on a pre-existing object.
Examples
Non-standard and deprecated way
var o = {}; o.__defineGetter__('gimmeFive', function() { return 5; }); console.log(o.gimmeFive); // 5
Standard-compliant ways
// Using the get operator var o = { get gimmeFive() { return 5; } }; console.log(o.gimmeFive); // 5 // Using Object.defineProperty var o = {}; Object.defineProperty(o, 'gimmeFive', { get: function() { return 5; } }); console.log(o.gimmeFive); // 5
Specifications
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Object.prototype.__defineGetter__()' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
__defineGetter__ | Chrome Full support 1 | Edge Full support 12 | Firefox
Full support
1
| IE Full support 11 | Opera Full support 9.5 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
- See implementation notes.
- See implementation notes.