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 :)")
1
  • 1
    Thankfully you cannot. Commented Sep 6, 2019 at 1:22

1 Answer 1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.