An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
May 26, 2022 07:54 am GMT
Original Link: https://dev.to/rajeshroyal/what-is-the-use-of-proxy-and-reflect-in-javascript-1a02
What is the use of Proxy and Reflect in JavaScript?
I have read the documentation for Proxy and Reflect on MDN but didn't understand much. Can anyone please explain or mention the real world use of these ES6
features.
- One of the good use of proxy I found is that we can declare our object values as private -
const object = { name: "Rajesh Royal", Age: 23, _Sex: "Male"}const logger = { get(target, property){ if(property.startsWith("_")){ throw new Error(`${property} is a private property.`); } console.log(`Reading the property ${property}`); return target[property]; }}const Logger = new Proxy(object, logger);// now if you try to access the private property it will throw an errorLogger._SexUncaught Error: _Sex is a private property. at Object.get (<anonymous>:4:19) at <anonymous>:1:8
Original Link: https://dev.to/rajeshroyal/what-is-the-use-of-proxy-and-reflect-in-javascript-1a02
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To