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() .

Interceptions

This trap can intercept these operations:

Invariants

If the following invariants are violated, the proxy will throw a TypeError :

  • The result of ownKeys() must be an array.
  • The type of each array element is either a String Symbol .
  • The result List must contain the keys of all non-configurable own properties of the target object.
  • If the target object is not extensible, then the result List must contain all the keys of the own properties of the target object and no other values.

范例

Trapping of getOwnPropertyNames

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.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
ownKeys Chrome 49 Edge 12 Firefox 18
18
In Firefox 42, the implementation got updated to reflect the final ES2015 specification: The result is now checked if it is an array and if the array elements are either of type string or of type symbol. Enumerating duplicate own property names is not a failure anymore.
IE No Opera 36 Safari 10 WebView Android 49 Chrome Android 49 Firefox Android 18
18
In Firefox 42, the implementation got updated to reflect the final ES2015 specification: The result is now checked if it is an array and if the array elements are either of type string or of type symbol. Enumerating duplicate own property names is not a failure anymore.
Opera Android 36 Safari iOS 10 Samsung Internet Android 5.0 nodejs 6.0.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: