出于完整性考虑,我觉得值得分享的是,至少你所要求的一些功能是
从技术上讲
可能吧,不过我
非常
建议探索一种不同的方法来解决潜在的问题。
使用
with
不建议使用该语句,因为它可能会导致令人困惑的错误和兼容性问题。
使用
with
声明和声明
Proxy
,有可能捕获任务,因为您实际上是在拦截
Environment Record
添加到范围中。具有
let
然而,这是绝对不可能的。
const handler = {
has(target, key) {
return typeof key === 'string';
},
set(target, key, value) {
console.log(key, 'has changed');
switch (typeof value) {
case 'function':
case 'object':
value = new Proxy(value, handler);
}
return Reflect.set(target, key, value);
}
};
with (new Proxy(Object.create(null), handler)) {
s = 'world';
x = 2;
o = { foo: 'bar' };
o.foo = 'bar2';
let v = []; // not trapped
}