I have the following proxy:
const p = new Proxy({}, { ownKeys(target) { return ['a', 'b']; }, });
MDN says that:
This trap can intercept these operations:
Object.getOwnPropertyNames()Object.getOwnPropertySymbols()Object.keys()Reflect.ownKeys()
Therefore, I expected Object.getOwnPropertyNames() and Object.keys() the same result. However, Object.getOwnPropertyNames(p) returns ['a', 'b'] (as expected), but Object.keys(p) returns an empty array. Why is this?
In addition, if I add a property to this object that is not returned by the ownKeys handler (for example, c ), it is ignored by both functions (they do not change their output). However, when I add a property that is returned by the ownKeys handler (for example, a ), Object.keys(p) now returns ['a'] .
Code snippet:
const p = new Proxy({}, { ownKeys(target) { return ['a', 'b']; }, }); console.log(Object.getOwnPropertyNames(p));
javascript ecmascript-6 es6-proxy
Michał Perłakowski
source share