3

I have an object, i want to intercept in opeartor access on the object.

e.g

myObject.operatorIn = ()=>throw new Error("You can't touch it :)")
Mars
  • 873
  • 1
  • 11
  • 24

1 Answers1

5

With a Proxy, you can create a has trap to intercept uses of in:

const myObject = {
  foo: 'foo'
};
const myObjectProxy = new Proxy(
  myObject,
  {
    has() {
      throw new Error("You can't touch it");
    }
  }
);
console.log(myObjectProxy.foo);
console.log('foo' in myObjectProxy);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320