Proxy in JS
Understanding JavaScript Proxies
Imagine when we were in school, there were days when we couldn't attend class, but we still wanted our attendance to be marked. In such cases, we would ask a friend to mark our attendance on our behalf. This friend acts as a proxy, taking actions on our behalf to ensure our attendance is recorded.
?? What is a Proxy?
In JavaScript, a Proxy is like that friend who marks your attendance. It's an object that wraps around another object and intercepts operations performed on that object. By using a Proxy, you can define custom behavior for fundamental operations such as getting, setting, or deleting properties of the object.
?? Why Use JavaScript Proxies?
?? How Does a Proxy Work?
A Proxy involves two main components
Creating a Proxy
To create a Proxy, you use the Proxy constructor, passing in the target object and a handler object with traps.
Traps
? get: Intercepts reading a property from the object.
? set: Intercepts writing a property to the object.
? ownKeys: Intercepts operations that list the properties of the object.
领英推荐
?? Explanation Using get, set & ownKeys for School Proxy??
Imagine you want to mark your attendance even when you're not in class. You ask your friend to do it for you. Similarly, in JavaScript:
By using a Proxy, you can ensure that every interaction with the object goes through your friend (the handler), allowing you to add custom behavior such as validation, logging, or access control.
? has: Intercepts the in operator used to check if a property exists in the object.
? deleteProperty: Intercepts the delete operator used to delete a property from the object.
Conclusion
? Purpose of Proxies:Proxies act like middlemen between code and objects. They let developers customize how basic operations like reading, writing, and deleting properties on objects work.
? Main Components:
? Common Traps and Their Uses:
? Benefits of Using Proxies:
Meanwhile what you all can do is, Like and Share this edition among your peers and also subscribe to this Newsletter so that you all can get notified when I come up with more content in future. Share this Newsletter with anyone who might be benefitted from this content.
Until next time, Dive Deep and Keep Learning! ??
Works On Java, Springboot, Micro service, Devops, Javascript, Reactjs, Angular, Nextjs, Nestjs, Cloud computing
8 个月Very informative
Building scalable web solutions | Fullstack Web Developer | Expert in React.js, Node.js and Express.js | Exploring Web3 | Open to collaborations and opportunities
8 个月Your articles are quite informative. Thanks for improving my JS knowledge!!
Core Professional in the field of HR & Administration/ Operation. Certified of Lean Competency . Life Time Learner .
8 个月Insightful!