Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 26, 2022 07:54 am GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To