handler.ownKeys(target) is a trap that runs when JavaScript wants the list of keys of a Proxy. This happens with Object.keys(), Object.getOwnPropertyNames(), Reflect.ownKeys(), JSON.stringify(), the spread syntax and the for...in loop.
The trap returns an array of strings and symbols. Inside the trap you can remove keys to hide them, or add keys. To get the real list, use Reflect.ownKeys(target).
Remember
const handler = {
ownKeys(target) { ... }
};
target → the original object
return an array of strings and symbols
no duplicates, no numbers
must include the non-configurable keys of the target
Syntax
Add an ownKeys trap to the handler of a Proxy.
JavaScript
const user = new Proxy({ name: "Ann", _token: "abc" }, {
ownKeys(target) {
return Reflect.ownKeys(target).filter((key) => key !== "_token");
}
});
console.log(Object.keys(user).join(", ")); // name
// ECMAScript 2015 (ES6)
handler.ownKeys(target)
// target : the original object
// returns : an array of strings and symbols
// throws : TypeError if the list breaks a rule
Parameters
Parameter
Description
target
The original object that the Proxy wraps.
Return Value
Return Value
Description
Array
A list of strings and symbols with no duplicates. It becomes the list of own keys of the Proxy.
TypeError (thrown)
The list has a number, a duplicate, or leaves out a non-configurable key of the target. If the target is not extensible, the list must hold exactly the keys of the target.
JavaScript Examples
Example 1 — Hide Private Keys from Every List
Remove the keys that start with an underscore from the list. Then Object.keys(), JSON.stringify() and for...in all agree.
Reflect.ownKeys: a, b
Object.keys: 0
Object.keys with both traps: a, b
How It Works
The list is made up.Reflect.ownKeys() returns exactly what the trap returns: a and b.
Object.keys checks each key. It asks whether each key is enumerable. The empty target has no such properties, so both keys are skipped and the length is 0.
Add the second trap. When getOwnPropertyDescriptor says the keys exist and are enumerable, Object.keys() includes them.
Example 3 — The Rules for the List
The list must hold only strings and symbols, with no duplicates, and it must keep the non-configurable keys of the target. The catch blocks print the error type only.
Duplicate keys: TypeError
Number keys: TypeError
Missing a fixed key: TypeError
How It Works
No duplicates. A key can only be listed once, so ["a", "a"] throws a TypeError.
Strings and symbols only. Property keys are never numbers. Return ["1", "2"] with quotes, not [1, 2].
Keep fixed keys. The property id is non-configurable, so it can never be hidden. A list without it throws a TypeError.
Compatibility
Browser & Runtime Support
handler.ownKeys() 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.ownKeys()
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.ownKeys()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 needs the list of own keys of a Proxy, for example for Object.keys(), Reflect.ownKeys(), JSON.stringify() and for...in. It returns an array of strings and symbols.
Return the real keys without it, for example Reflect.ownKeys(target).filter(...). Object.keys() starts from your list, so the key disappears from it, and from JSON.stringify() and for...in too.
Object.keys() checks each key with getOwnPropertyDescriptor and skips the ones that do not exist on the target or are not enumerable. Add a getOwnPropertyDescriptor trap that returns an enumerable descriptor for them.
It must be an array-like list of strings and symbols, with no duplicates. It must include every non-configurable key of the target, and if the target is not extensible, it must contain exactly the keys of the target.
No. It only changes the list of keys. Reading a value uses the get trap, so hiding a key from the list does not stop someone who knows its name from reading it.