Proxy Object in JavaScript
Level 4283 keystrokes
Proxy allows intercepting and customizing operations on objects.
JavaScript code
// Define a handler object with traps1const handler = { // get trap: called when reading a property2 get(target, prop) {3 console.log(`Getting ${prop}`);4 return target[prop];5 }, // set trap: called when setting a property6 set(target, prop, value) {7 console.log(`Setting ${prop} to ${value}`);8 target[prop] = value; // Must return true to indicate success9 return true;10 }11};// Create a proxy wrapping an empty object12const obj = new Proxy({}, handler);// Setting triggers the set trap13obj.name = "Alice";// Getting triggers the get trap14console.log(obj.name);Output
Setting name to Alice Getting name Alice
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
;× 9{× 7}× 7(× 6)× 6,× 5.× 5`× 4=× 4$× 3