The handler.ownKeys()
method is a trap for Reflect.ownKeys()
.
var p = new Proxy(target, { ownKeys: function(target) { } });
The following parameter is passed to the ownKeys
method. this
is bound to the handler.
target
The ownKeys
method must return an enumerable object.
The handler.ownKeys()
method is a trap for Reflect.ownKeys()
.
This trap can intercept these operations:
If the following invariants are violated, the proxy will throw a TypeError
:
ownKeys
must be an array.String
or a Symbol
.The following code traps Object.getOwnPropertyNames()
.
var p = new Proxy({}, { ownKeys: function(target) { console.log('called'); return ['a', 'b', 'c']; } }); console.log(Object.getOwnPropertyNames(p)); // "called" // [ 'a', 'b', 'c' ]
The following code violates an invariant.
var obj = {}; Object.defineProperty(obj, 'a', { configurable: false, enumerable: true, value: 10 } ); var p = new Proxy(obj, { ownKeys: function(target) { return [123, 12.5, true, false, undefined, null, {}, []]; } }); console.log(Object.getOwnPropertyNames(p)); // TypeError: proxy [[OwnPropertyKeys]] must return an array // with only string and symbol elements
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of '[[OwnPropertyKeys]]' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of '[[OwnPropertyKeys]]' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 49 | 12 | 18
|
No | 36 | 10 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 49 | 49 | Yes | 18
|
36 | 10 | 5.0 |
Server | |
---|---|
Node.js | |
Basic support | 6.0.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/ownKeys