W3cubDocs

/JavaScript

Proxy.proxy.ownKeys

The handler.ownKeys() method is a trap for Reflect.ownKeys().

Syntax

const p = new Proxy(target, {
  ownKeys: function(target) {
  }
});

Parameters

The following parameter is passed to the ownKeys() method. this is bound to the handler.

target
The target object.

Return value

The ownKeys() method must return an enumerable object.

Description

The 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 or a 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.

Examples

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

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
ownKeys 49 12 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.
No 36 10
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
ownKeys 49 49 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.
36 10 5.0
Server
Node.js
ownKeys 6.0.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/ownKeys