use*_*159 3 javascript node.js
我有一个对象,我想拦截in对该对象的操作者访问。
例如
myObject.operatorIn = ()=>throw new Error("You can't touch it :)")
Run Code Online (Sandbox Code Playgroud)
使用代理,您可以创建一个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)
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |