Proxy深度检测
const deepProxy = (target, handler) => {const proxy = new Proxy(target, handler);
for (let key in target) {
if (typeof target === 'object') {
target = deepProxy(target, 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 \nSetting bar to \nSetting baz to world
页:
[1]