handler.ownKeys()
method is a trap for
Reflect.ownKeys()
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
const p = new Proxy(target, {
ownKeys: function(target) {
}
});
The following parameter is passed to the
ownKeys()
方法。
this
is bound to the handler.
target
The target object.
ownKeys()
method must return an enumerable object.
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
或
Symbol
.
The following code traps
Object.getOwnPropertyNames()
.
const 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.
const obj = {};
Object.defineProperty(obj, 'a', {
configurable: false,
enumerable: true,
value: 10 }
);
const 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
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of '[[OwnPropertyKeys]]' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ownKeys
|
Chrome 49 | Edge 12 |
Firefox
18
|
IE No | Opera 36 | Safari 10 | WebView Android 49 | Chrome Android 49 |
Firefox Android
18
|
Opera Android 36 | Safari iOS 10 | Samsung Internet Android 5.0 | nodejs 6.0.0 |
完整支持
不支持
见实现注意事项。