JavaScript Proxy a first introduction

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Recently I had my first production use-case for the Proxy object. So let me tell you a bit more about it and how you can use it in your code.
A Proxy object is a method that copies an object and can apply overwrites to specific functions.
This is a great way to intercept essential object functions like get or set.
Let's start with a basic object definition.
const user = {
firstName: 'Chris',
lastName: 'Bongers',
age: 10,
};
Now, let's proxy this object.
const proxyUser = new Proxy(user, handler);
The Proxy object takes two arguments, the first one is the original object and the second one is a handler. The handler defines which operations will be intercepted or modified.
Let's say we want to modify the get function.
const handler = {
get(target, prop) {
console.log('get called', target, prop);
return target[prop].toUpperCase();
},
};
In the above example, we log what is being called and modify the get always to return an uppercase variant.
Let's now try to get the first name of the person.
console.log(proxyUser.firstName);
console.log(proxyUser.lastName);
// Returns: CHRIS BONGERS
Pretty neat!
Let's quickly take a closer look at the variables we received. The target holds the entire object, and the prop has the one that is being changed.
You can also only act on specific properties.
const handler = {
get(target, prop) {
if (prop === 'firstName') {
return target[prop].toUpperCase();
}
return target[prop];
},
};
console.log(proxyUser.firstName);
console.log(proxyUser.lastName);
// Returns: CHRIS Bongers
In this case, only the firstName property will be returned in uppercase.
We have seen the get handler being used, but there are more handlers we can leverage.
These handlers are often called traps, as they trap calls to the target object.
Here is a list of the traps we can overwrite.
applyconstructdeletePropertydefinePropertyenumerategetgetPrototypeOfgetOwnPropertyDescriptorhasisExtensibleownKeyspreventExtensionssetsetPrototypeOfEach handler comes with its own set of properties. You can find the complete list on MDN.
You can also try out the sample I prepared in this CodePen.
We can use the Proxy object to overwrite or catch specific object actions.
By using proxies, we can leverage some normally inaccessible handlers. These handlers can then be applied to all or some of the properties of an object.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter