Development & Coding

JavaScript Proxies, a Lesser Known Super Power, another article by ChatGPT

The Prompt

I run a blog called Something Decent. The blog aims to provide users with friendly articles based on varying subjects from web dev advice to cooking recipes but mostly focuses on web dev. Today I would like you to write an article for Something Decent. The article should explain, in an ELI5 fashion, how to use the Proxy class in JavaScript. The article should be titled “JavaScript Proxies, a Lesser Known Super Power” and should use the following situation as the crux of its examples within the article:

1. There is an object titled animals
2. The object has the following properties: name, age, species
3. The proxy will alter the set method of the animals object
4. The proxy will seek to dispatch an event called ‘animal-age-changed’ when the setter of the animals object attempts to change the age method of it

The article will assume the reader has limited knowledge of JS proxies and their inner workings. It will also be written in a light hearted tone with a focus on seeming friendly.

The Article

Are you ready to learn about one of JavaScript’s lesser known super powers? It’s called a Proxy and it can help you do some pretty cool things with your objects. Let’s dive in and take a look at how it works using the following situation as an example:

Imagine you have an object called animals that has the following properties: name, age, and species. Now, let’s say you want to use a Proxy to alter the set method of the animals object. Specifically, you want the Proxy to dispatch an event called animal-age-changed whenever the setter of the animals object attempts to change the age property.

To do this, we first need to create our Proxy. We do this using the Proxy constructor and passing in our animals object as the first argument, and an object containing our desired behavior as the second argument. In this case, we want to intercept the set method, so we use the set property of the second object to define a function that will handle this behavior.

Here’s what that might look like:

const handler = {
  set(target, property, value, receiver) {
    // Do something here
  }
}

const animals = { name: 'Fluffy', age: 3, species: 'dog' }
const animalProxy = new Proxy(animals, handler)

Now that we have our Proxy set up, we can add the behavior we want it to have. In this case, we want it to dispatch an event whenever the set method is called on the animals object. To do this, we can check if the property being set is the age property, and if it is, we can dispatch our event using the dispatchEvent method.

Here’s what that might look like:

const handler = {
  set(target, property, value, receiver) {
    if (property === 'age') {
      const event = new Event('animal-age-changed')
      target.dispatchEvent(event)
    }
  }
}

const animals = { name: 'Fluffy', age: 3, species: 'dog' }
const animalProxy = new Proxy(animals, handler)

Now, whenever the set method is called on the animals object and the property being set is age, our Proxy will dispatch an event called animal-age-changed.

But wait, there’s more! Proxies can do even more than just intercept methods like set. They can also be used to provide default values for properties that don’t exist, or to create a read-only version of an object. The possibilities are endless!

So there you have it, a quick and friendly introduction to JavaScript Proxies. They may not be as well known as some of JavaScript’s other features, but they definitely pack a punch and are worth taking the time to learn about. Happy coding!