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.
obj.__defineGetter__(prop, func)
prop
func
The __defineGetter__
allows a getter to be defined on a pre-existing object.
var o = {}; o.__defineGetter__('gimmeFive', function() { return 5; }); console.log(o.gimmeFive); // 5
// 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
Specification |
---|
ECMAScript (ECMA-262) The definition of 'Object.prototype.__defineGetter__()' in that specification. |
Desktop | ||||||
---|---|---|---|---|---|---|
__defineGetter__
|
1 | 12 | 1
|
11 | 9.5 | 3 |
Mobile | ||||||
---|---|---|---|---|---|---|
__defineGetter__
|
1 | 18 | 4 | 10.1 | 1 | 1.0 |
Server | |
---|---|
__defineGetter__
|
Yes |
Object.prototype.__defineSetter__()
get
operatorObject.defineProperty()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__