Understanding JavaScript Proxies: A Dive into Dynamic Object Interception

Aaron Billings
2 min readNov 19, 2023

JavaScript, as a versatile and dynamic language, offers advanced features that empower developers to manipulate and control objects in unique ways. One such feature is the Proxy object, which allows for the creation of custom behaviors when interacting with objects.

In this article, we’ll explore a practical example of using the Proxy object to intercept and handle property access and assignment.

Code Overview:

The provided code snippet showcases a simple scenario using a Proxy to intercept operations on a pizza object. (I used pizza for an example here because that’s my favorite food and I was hungry at the time of writing this)

The handler object defines custom behavior for the get and set operations, enabling us to log messages and modify the object accordingly.

const handler = {
get: function(target, property) {
console.log(`Getting property ${property}`);
return target[property];
},
set: function(target, property, value) {
console.log(`Setting property ${property} to ${value}`);
target[property] = value;
return true; // indicates that the setting has been done successfully
}
};

const pizza = { name: 'Margherita', toppings: ['tomato sauce', 'mozzarella'] };
const proxiedPizza = new Proxy(pizza, handler);

console.log(proxiedPizza.name); // Outputs "Getting property name" and "Margherita"
proxiedPizza.name = 'Pepperoni';

Understanding the handler Object:

  • Get Operation:

The get method is triggered when attempting to access a property of the proxied object.

It logs a message indicating the property being accessed and then returns the corresponding value from the target object.

get: function(target, property) {
console.log(`Getting property ${property}`);
return target[property];
},
  • Set Operation:

The set method is invoked when assigning a value to a property of the proxied object.

It logs a message detailing the property and the assigned value, updates the target object, and returns true to indicate a successful assignment.

set: function(target, property, value) {
console.log(`Setting property ${property} to ${value}`);
target[property] = value;
return true; // indicates that the setting has been done successfully
}

Creating the Proxy:

The pizza object, representing a Margherita pizza, is then proxied using the handler. The resulting proxiedPizza object inherits the behaviors defined in the handler for property access and assignment.

const pizza = { name: 'Margherita', toppings: ['tomato sauce', 'mozzarella'] };
const proxiedPizza = new Proxy(pizza, handler);

The code above demonstrates the proxy in action by accessing and modifying the name property of the pizza.

When proxiedPizza.name is accessed, it triggers the get operation, logging a message and returning the current value, “Margherita.” Subsequently, setting proxiedPizza.name to “Pepperoni” invokes the set operation, logging the change and updating the property.

JavaScript Proxies offer a powerful mechanism for intercepting and customizing object operations. This example provides a basic understanding of how Proxies can be employed to control and enhance object behavior dynamically. It can also be used to add reactivity to your objects.

Integrating Proxies into your codebase can lead to cleaner, more maintainable code by centralizing logic related to object interactions. As you delve deeper into JavaScript, exploring features like Proxies opens up new possibilities for crafting efficient and expressive code.

--

--

Aaron Billings

Full Stack developer that follows the ABCs (Always Be Coding). If I’m not coding you can find me playing video games, or spending time with family.