I have created deep proxy object. Now I want that whenever someone access a deep property it must show the path.
const deepPath = [];
function deepProxy(obj, path = []) {
const store = {};
return new Proxy(obj, {
deleteProperty(obj, prop) {
delete obj[prop];
delete store[prop];
return true;
},
get(obj, prop) {
if (store[prop]) return store[prop];
const value = obj[prop];
if (isObject(value))
return (store[prop] = deepProxy(value, [...path, prop]));
return value;
},
set(obj, prop, value) {
if (isObject(value))
value = store[prop] = deepProxy(value, [...path, prop]);
obj[prop] = value;
return true;
}
});
}
const obj = deepProxy({
first: {
second: {
deep: true
}
}
});
Now it is deep proxy. Now if someone access a deep object or value. then how i can know path. for exaMPLE. Whenever i access a value it must push full path into "deepPath" array.
console.log(obj.first.second);
// should push "first.second"
console.log(obj.first);
// should push "first"
console.log(obj.first.second.deep);
// should push "first.second.deep"
Is there any way to do this ?