I have this JavaScript Proxy code which I want to trigger the set handler that can tell me the first and second keys.
This means that when I try to set globalVar.a.b = "55b" only the line prop == "a.b" would be triggered. Now it's trigger only using the 2nd key.
let globalVar = [];
const handler = {
get(target, name) {
const v = name in target ? target[name] : (target[name] = {});
return typeof v == "object" ? new Proxy(v, handler) : v;
},
set(target, prop, val) {
if (prop == "a") {
console.log("myObject[" + prop + "] setto " + val);
} else if (prop == "b") { //getting triggered
console.log("myObject[" + prop + "] setto " + val);
} else if (prop == "c") { //getting triggered
console.log("myObject[" + prop + "] setto " + val);
} else if (prop == "a.b") { //want to get something like this
console.log("myObject[" + prop + "] setto " + val);
}
target[prop] = val;
}
};
globalVar = new Proxy(globalVar, handler);
globalVar.a.x = "55a";
globalVar.a.b = "55b";
globalVar.a.c = "et4";