DEV Community

loading...
Cover image for Proxy Pattern in JavaScript

Proxy Pattern in JavaScript

Beto Muniz
Indie Creator @ listtta.com ~ seujob.tech ~ protocoloalterado.com.br • OSS Crafter • Writing about Web Development
・1 min read

Just a catch up about how the Proxy object works on JavaScript to allow us to implement Proxy patterns.

📌 JavaScript's Proxy object allows us to intercept and modifies any JavaScript object.

📌 JavaScript's Proxy object is an elegant and safe way for creating or extending libraries, caching, error handling, and complex data manipulation on JavaScript.

const obj = {a: 1, b: 2};

const arrProxy = new Proxy(obj, {
  get: function (item, property) {
    if (item.hasOwnProperty(property)) return item[property];

    return "default value";
  },
});

arrProxy.z; // "default value"
arrProxy.a; // 1
Enter fullscreen mode Exit fullscreen mode

💡 What are the use cases that you most liked to use such a JavaScript feature?

📚 Still, for a detailed API spec of Proxy's object in JavaScript, take a look in the MDN docs.

💌 If you thought this brief content useful for your web dev studies and want to receive more, subscribe to my newsletter

Discussion (0)

Forem Open with the Forem app