如何在javascript中拦截“输入”运算符?

use*_*159 3 javascript node.js

我有一个对象,我想拦截in对该对象的操作者访问。

例如

myObject.operatorIn = ()=>throw new Error("You can't touch it :)")
Run Code Online (Sandbox Code Playgroud)

Cer*_*nce 5

使用代理,您可以创建一个has陷阱来拦截对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);
Run Code Online (Sandbox Code Playgroud)