handler.getOwnPropertyDescriptor()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
The handler.getOwnPropertyDescriptor() method is a trap for the [[GetOwnProperty]] object internal method, which is used by operations such as Object.getOwnPropertyDescriptor().
Try it
const monster = {
eyeCount: 4,
};
const handler = {
getOwnPropertyDescriptor(target, prop) {
console.log(`called: ${prop}`);
// Expected output: "called: eyeCount"
return { configurable: true, enumerable: true, value: 5 };
},
};
const proxy = new Proxy(monster, handler);
console.log(Object.getOwnPropertyDescriptor(proxy, "eyeCount").value);
// Expected output: 5
Syntax
new Proxy(target, {
getOwnPropertyDescriptor(target, property) {
}
})
Parameters
The following parameters are passed to the getOwnPropertyDescriptor() method. this is bound to the handler.
Return value
The getOwnPropertyDescriptor() method must return an object or undefined, representing the property descriptor. Missing attributes are normalized in the same way as Object.defineProperty().
Description
>Interceptions
This trap can intercept these operations:
Or any other operation that invokes the [[GetOwnProperty]] internal method.
Invariants
The proxy's [[GetOwnProperty]] internal method throws a TypeError if the handler definition violates one of the following invariants:
- The result must be either an
Objectorundefined. - A property cannot be reported as non-existent, if it exists as a non-configurable own property of the target object. That is, if
Reflect.getOwnPropertyDescriptor()returnsconfigurable: falsefor the property ontarget, then the trap must not returnundefined. - A property cannot be reported as non-existent, if it exists as an own property of a non-extensible target object. That is, if
Reflect.isExtensible()returnsfalsefor the target object, then the trap must not returnundefined. - A property cannot be reported as existent, if it does not exist as an own property of the target object and the target object is not extensible. That is, if
Reflect.isExtensible()returnsfalsefor the target object, andReflect.getOwnPropertyDescriptor()returnsundefinedfor the property ontarget, then the trap must returnundefined. - A property cannot be reported as non-configurable, unless it exists as a non-configurable own property of the target object. That is, if
Reflect.getOwnPropertyDescriptor()returnsundefinedorconfigurable: truefor the property ontarget, then the trap must not returnconfigurable: false. - A property cannot be reported as both non-configurable and non-writable, unless it exists as a non-configurable, non-writable own property of the target object. That is, in addition to the previous invariant, if
Reflect.getOwnPropertyDescriptor()returnsconfigurable: false, writable: truefor the property ontarget, then the trap must not returnconfigurable: false, writable: false. - If a property has a corresponding property on the target object, then the target object property's descriptor must be compatible with
descriptor. That is, pretendingtargetis an ordinary object, thenObject.defineProperty(target, property, resultObject)must not throw an error. TheObject.defineProperty()reference contains more information, but to summarize, when the target property is non-configurable, the following must hold:configurable,enumerable,get, andsetmust be the same as original.writablemust also be the original by virtue of the previous invariant.- the property must stay as data or accessor
- the
valueattribute can only be changed ifwritableistrue
Examples
>Trapping of getOwnPropertyDescriptor
The following code traps Object.getOwnPropertyDescriptor().
const p = new Proxy(
{ a: 20 },
{
getOwnPropertyDescriptor(target, prop) {
console.log(`called: ${prop}`);
return { configurable: true, enumerable: true, value: 10 };
},
},
);
console.log(Object.getOwnPropertyDescriptor(p, "a").value);
// "called: a"
// 10
The following code violates an invariant.
const obj = { a: 10 };
Object.preventExtensions(obj);
const p = new Proxy(obj, {
getOwnPropertyDescriptor(target, prop) {
return undefined;
},
});
Object.getOwnPropertyDescriptor(p, "a"); // TypeError is thrown
Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p> |
Browser compatibility
Loading…