Proxy

 Proxy


Proxies enable you to intercept and customize operations performed on objects, such as accessing properties, invoking methods, etc.



const person = {

  name: 'John Doe',

  age: 30

};


const personProxy = new Proxy(person, {

  get(target, property) {

    console.log(`Getting ${property}`);

    return target[property];

  },

  set(target, property, value) {

    console.log(`Setting ${property} to ${value}`);

    target[property] = value;

  }

});


console.log(personProxy.name); // Output: Getting name, John Doe

personProxy.age = 35; // Output: Setting age to 35

In the example above, a Proxy is created for the person object. The get and set traps intercept property access and assignment operations and allow you to add custom behavior or perform additional actions.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...