Skip to main content

Command Palette

Search for a command to run...

Proxy Pattern in JavaScript

Updated
โ€ข1 min read
Proxy Pattern in JavaScript
B
Building AI-powered experiences at Grafana Labs

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

๐Ÿ’ก 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.

More from this blog

Beto Muniz

15 posts

Proxy Pattern in JavaScript