Why isn't ownKeys Proxy trap working with Object.keys()?

1.1k Views Asked by At

In the documentation of the Proxy ownKeys trap on MDN it states that it will intercept Object.keys() calls:

This trap can intercept these operations:

Object.getOwnPropertyNames()

Object.getOwnPropertySymbols()

Object.keys()

Reflect.ownKeys()

However, from my tests it doesn't seem to work with Object.keys:

const proxy = new Proxy({}, {
  ownKeys() {
    console.log("called")
    return ["a", "b", "c"]
  }
})

console.log(Object.keys(proxy))

console.log(Object.getOwnPropertyNames(proxy))

console.log(Reflect.ownKeys(proxy))

3

There are 3 best solutions below

0
On BEST ANSWER

The reason is simple: Object.keys returns only properties with the enumerable flag. To check for it, it calls the internal method [[GetOwnProperty]] for every property to get its descriptor. And here, as there’s no property, its descriptor is empty, no enumerable flag, so it’s skipped.

For Object.keys to return a property, we need it to either exist in the object, with the enumerable flag, or we can intercept calls to [[GetOwnProperty]] (the trap getOwnPropertyDescriptor does it), and return a descriptor with enumerable: true.

Here’s an example of that:

let user = { };

user = new Proxy(user, {
  ownKeys(target) { // called once to get a list of properties
    return ['a', 'b', 'c'];
  },

  getOwnPropertyDescriptor(target, prop) { // called for every property
    return {
      enumerable: true,
      configurable: true
      /* ...other flags, probable "value:..." */
    };
  }

});

console.log( Object.keys(user) ); // ['a', 'b', 'c']

Source

0
On

Object.keys returns only the enumerable own properties of an object. Your proxy doesn't have such, or at least it doesn't report them in its getOwnPropertyDescriptor trap. It works with

const proxy = new Proxy({}, {
  ownKeys() {
    console.log("called ownKeys")
    return ["a", "b", "c"]
  },
  getOwnPropertyDescriptor(target, prop) {
    console.log(`called getOwnPropertyDescriptor(${prop})`);
    return { configurable: true, enumerable: true };
  } 
})

console.log(Object.keys(proxy))

console.log(Object.getOwnPropertyNames(proxy))

console.log(Reflect.ownKeys(proxy))

2
On
const proxy = new Proxy({}, {
ownKeys: function () {
  console.log("called")
  return ["a", "b", "c"]
  }
 });