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
Syntax
new Proxy(target, {
getOwnPropertyDescriptor(target, property) {
}
})
Parameters
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
Object
orundefined
. - 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: false
for 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()
returnsfalse
for 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()
returnsfalse
for the target object, andReflect.getOwnPropertyDescriptor()
returnsundefined
for 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()
returnsundefined
orconfigurable: true
for 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: true
for 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, pretendingtarget
is 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
, andset
must be the same as original.writable
must also be the original by virtue of the previous invariant.- the property must stay as data or accessor
- the
value
attribute can only be changed ifwritable
istrue
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 Language Specification # sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p |
Browser compatibility
BCD tables only load in the browser