马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
const deepProxy = (target, handler) => {
const proxy = new Proxy(target, handler);
for (let key in target) {
if (typeof target[key] === 'object') {
target[key] = deepProxy(target[key], handler);
}
}
return proxy;
}
const obj = {
foo: {
bar: {
baz: 'hello'
}
}
};
const handler = {
get(target, key, receiver) {
console.log(`Getting ${key}`);
return Reflect.get(target, key, receiver);
},
set(target, key, value, receiver) {
console.log(`Setting ${key} to ${value}`);
return Reflect.set(target, key, value, receiver);
}
};
const proxy = deepProxy(obj, handler);
console.log(proxy.foo.bar.baz); // Output: Getting foo\nGetting bar\nGetting baz\nhello
proxy.foo.bar.baz = 'world'; // Output: Setting foo to [object Object]\nSetting bar to [object Object]\nSetting baz to world
|