Object legacy accessors (__defineGetter__ …)
Netscape shipped these before the language had a property-descriptor model. They still work everywhere, they are normatively optional, and defineProperty does all four jobs properly.
Demo
The pair is the object own keys and the value read back. Note that the key IS listed — these legacy methods create an enumerable property, which is the one behavioural difference from Object.defineProperty, whose enumerable flag defaults to false. Compare with the defineProperty demo, where the equivalent call produces an empty key list. That difference is the only practical reason to know these exist beyond recognising them in old code.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| key | string | symbol | yes | The property key. All four take this as their first argument. |
| accessor | Function | no (none) | Only the define pair take a second argument — the getter or setter function. A non-callable value is a TypeError. |
Return value
varies — The define pair return undefined and modify the object. The lookup pair return the accessor function, or undefined if there is none.
Common patterns
Object.defineProperty(o, "a", { get: () => 7, enumerable: true, configurable: true, });
const o = { get a() { return 7; } };
Object.getOwnPropertyDescriptor(o, "a")?.get;
Examples
Pitfalls
o.__defineGetter__("a", f)
Object.defineProperty(o, "a", {get: f})
Object.defineProperty(o, "a", {get: f})
Object.defineProperty(o, "a", {get: f, enumerable: true, configurable: true})
Object.create(null).__defineGetter__("a", f)
Object.defineProperty(Object.create(null), "a", {get: f})
const p = {get a() { return 1; }}; Object.create(p).__lookupGetter__("a")
Object.getOwnPropertyDescriptor(Object.create(p), "a")
When to use
- Never in new code
- Recognising them while reading something written before 2009
- Defining an accessor → Object.defineProperty, or a get/set literal
- Reading an accessor → Object.getOwnPropertyDescriptor
- Portable code → Annex B is optional outside browsers
- Null-prototype objects → the methods are not there
Notes
FAQ
__defineGetter__ and __defineSetter__ attach an accessor to a property; __lookupGetter__ and __lookupSetter__ retrieve one. Object.defineProperty covers the first two and getOwnPropertyDescriptor covers the last two.
o.__defineGetter__("a", () => 1); Object.defineProperty(o, "a", {get: () => 1}); // the same thing