handler.getOwnPropertyDescriptor(target, property) is a trap that runs when JavaScript asks a Proxy for the descriptor of one of its own properties. A descriptor is a small object that says what a property is: its value and whether it is writable, enumerable and configurable.
It runs for Object.getOwnPropertyDescriptor() and Reflect.getOwnPropertyDescriptor(), but also behind the scenes for hasOwnProperty(), Object.keys(), JSON.stringify() and the spread syntax. The trap returns a descriptor object, or undefined to say that the property does not exist.
Remember
const handler = {
getOwnPropertyDescriptor(target, property) { ... }
};
target → the original object
property → the name (a string or a symbol)
return an object → the property exists, with this descriptor
return undefined → the property does not exist
Syntax
Add a getOwnPropertyDescriptor trap to the handler of a Proxy.
JavaScript
const user = { name: "Ann", _id: 7 };
const view = new Proxy(user, {
getOwnPropertyDescriptor(target, key) {
if (key === "_id") {
return undefined; // hide _id
}
return Reflect.getOwnPropertyDescriptor(target, key);
}
});
console.log(Object.keys(view).join(", ")); // name
Call
What it does
Object.getOwnPropertyDescriptor(proxy, "a")
Asks directly for a descriptor. Runs the trap.
proxy.hasOwnProperty("a")
Checks if the property is an own property. Runs the trap.
Object.keys(proxy), JSON.stringify(proxy)
Both ask for each key's descriptor to see if it is enumerable. They run the trap.
// ECMAScript 2015 (ES6)
handler.getOwnPropertyDescriptor(target, property)
// target : the original object
// property : a string or a symbol
// returns : a descriptor object, or undefined
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
property
The name of the property whose descriptor is wanted: a string or a symbol.
Return Value
Return Value
Description
Object
A descriptor such as { value: 1, writable: true, enumerable: true, configurable: true }. The property counts as existing.
undefined
The property counts as not existing.
TypeError (thrown)
The trap returned something else, or broke a rule about what the target really has.
JavaScript Examples
Example 1 — Hide Properties from Keys and JSON
Return undefined for names that start with an underscore. They then disappear from hasOwnProperty(), Object.keys() and JSON.stringify().
Through proxy: {"value":1,"writable":false,"enumerable":true,"configurable":true}
Real object: {"value":1,"writable":true,"enumerable":true,"configurable":true}
Missing property: undefined
How It Works
Get the real descriptor.Reflect.getOwnPropertyDescriptor() returns a new descriptor object, so the trap can edit it safely.
Change one field. The trap sets writable to false before returning it. This is allowed because the real property is configurable.
Missing stays missing. For z, the real answer is undefined, and the trap passes it on unchanged.
Example 3 — A Virtual Property and the Rules
The trap can say that a property exists even when the target does not have it. Then see that it cannot hide a frozen property. The catch block prints the error type only.
Has own id: true
Descriptor value: 99
Direct read: undefined
Keys: 0
Hiding a frozen property: TypeError
How It Works
A made-up descriptor. For id, the trap returns a full descriptor, so hasOwnProperty("id") says true and the descriptor value is 99. The target stays empty.
Other traps are separate. A direct read uses the get trap, which does not exist here, so virtual.id is undefined. Object.keys() takes its names from the ownKeys trap, so the list is empty.
No lying about frozen.x is non-configurable. Saying it does not exist is caught by JavaScript, which throws a TypeError.
Compatibility
Browser & Runtime Support
handler.getOwnPropertyDescriptor() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.getOwnPropertyDescriptor()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.getOwnPropertyDescriptor()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when JavaScript asks a Proxy for the descriptor of an own property. It returns a descriptor object, or undefined if the property should count as missing.
Object.getOwnPropertyDescriptor() and Reflect.getOwnPropertyDescriptor() directly, and also hasOwnProperty(), Object.keys(), Object.entries(), JSON.stringify(), the spread syntax and Object.assign(), which ask for descriptors internally.
Return undefined from this trap for that property. Object.keys() asks for the descriptor of each key to see if it is enumerable, so it skips the ones that do not exist. You can also use the ownKeys trap to remove the key from the list.
No. Reading a value uses the get trap. This trap only answers questions about the property itself, such as whether it exists and whether it is writable or enumerable.
Most likely it returned something that is not an object or undefined, or it broke a rule. For example, it cannot report a non-configurable property of the target as missing, and it cannot add new properties to a target that is not extensible.