OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

proxy for 3d array, knowing the two indexes of array inside the handler function

  • Thread starter Thread starter dede
  • Start date Start date
D

dede

Guest
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.


Code:
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";

<p>I have this JavaScript Proxy code which I want to trigger the <code>set</code> handler that can tell me the first and second keys.</p>
<p>This means that when I try to set <code>globalVar.a.b = "55b"</code> only the line <code>prop == "a.b"</code> would be triggered. Now it's trigger only using the 2nd key.</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>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";</code></pre>
</div>
</div>
</p>
 
Top