Interested in our next book? Learn more about Building Large-scale JavaScript Web Apps with React

Design Pattern

Proxy Pattern

With a Proxy object, we get more control over the interactions with certain objects. A proxy object can determine the behavior whenever we’re interacting with the object, for example when we’re getting a value, or setting a value.


Generally speaking, a proxy means a stand-in for someone else. Instead of speaking to that person directly, you’ll speak to the proxy person who will represent the person you were trying to reach. The same happens in JavaScript: instead of interacting with the target object directly, we’ll interact with the Proxy object.


Let’s create a person object, that represents John Doe.

const person = {
  name: "John Doe",
  age: 42,
  nationality: "American",
};

Instead of interacting with this object directly, we want to interact with a proxy object. In JavaScript, we can easily create a new proxy by creating a new instance of Proxy.

const person = {
  name: "John Doe",
  age: 42,
  nationality: "American",
};

const personProxy = new Proxy(person, {});

The second argument of Proxy is an object that represents the handler. In the handler object, we can define specific behavior based on the type of interaction. Although there are many methods that you can add to the Proxy handler, the two most common ones are get and set:

  • get: Gets invoked when trying to access a property
  • set: Gets invoked when trying to modify a property

Effectively, what will end up happening is the following:

Instead of interacting with the person object directly, we’ll be interacting with the personProxy.

Let’s add handlers to the personProxy Proxy. When trying to modify a property, thus invoking the set method on the Proxy, we want the proxy to log the previous value and the new value of the property. When trying to access a property, thus invoking the get method on the Proxy, we want the proxy to log a more readable sentence that contains the key and value of the property.

const personProxy = new Proxy(person, {
  get: (obj, prop) => {
    console.log(`The value of ${prop} is ${obj[prop]}`);
  },
  set: (obj, prop, value) => {
    console.log(`Changed ${prop} from ${obj[prop]} to ${value}`);
    obj[prop] = value;
  },
});

Perfect! Let’s see what happens when we’re trying to modify or retrieve a property.

index.js
1const person = {
2 name: "John Doe",
3 age: 42,
4 nationality: "American"
5};
6
7const personProxy = new Proxy(person, {
8 get: (obj, prop) => {
9 console.log(`The value of ${prop} is ${obj[prop]}`);
10 },
11 set: (obj, prop, value) => {
12 console.log(`Changed ${prop} from ${obj[prop]} to ${value}`);
13 obj[prop] = value;
14 return true;
15 }
16});
17
18personProxy.name;
19personProxy.age = 43;

When accessing the name property, the Proxy returned a better sounding sentence: The value of name is John Doe.

When modifying the age property, the Proxy returned the previous and new value of this property: Changed age from 42 to 43.


A proxy can be useful to add validation. A user shouldn’t be able to change person’s age to a string value, or give them an empty name. Or if the user is trying to access a property on the object that doesn’t exist, we should let the user know.

const personProxy = new Proxy(person, {
  get: (obj, prop) => {
    if (!obj[prop]) {
      console.log(
        `Hmm.. this property doesn't seem to exist on the target object`
      );
    } else {
      console.log(`The value of ${prop} is ${obj[prop]}`);
    }
  },
  set: (obj, prop, value) => {
    if (prop === "age" && typeof value !== "number") {
      console.log(`Sorry, you can only pass numeric values for age.`);
    } else if (prop === "name" && value.length < 2) {
      console.log(`You need to provide a valid name.`);
    } else {
      console.log(`Changed ${prop} from ${obj[prop]} to ${value}.`);
      obj[prop] = value;
    }
  },
});

Let’s see what happens when we’re trying to pass faulty values!

index.js
1const person = {
2 name: "John Doe",
3 age: 42,
4 nationality: "American"
5};
6
7const personProxy = new Proxy(person, {
8 get: (obj, prop) => {
9 if (!obj[prop]) {
10 console.log(`Hmm.. this property doesn't seem to exist`);
11 } else {
12 console.log(`The value of ${prop} is ${obj[prop]}`);
13 }
14 },
15 set: (obj, prop, value) => {
16 if (prop === "age" && typeof value !== "number") {
17 console.log(`Sorry, you can only pass numeric values for age.`);
18 } else if (prop === "name" && value.length < 2) {
19 console.log(`You need to provide a valid name.`);
20 } else {
21 console.log(`Changed ${prop} from ${obj[prop]} to ${value}.`);
22 obj[prop] = value;
23 }
24 return true;
25 }
26});
27
28personProxy.nonExistentProperty;
29personProxy.age = "44";
30personProxy.name = "";

The proxy made sure that we weren’t modifying the person object with faulty values, which helps us keep our data pure!


Reflect

JavaScript provides a built-in object called Reflect, which makes it easier for us to manipulate the target object when working with proxies.

Previously, we tried to modify and access properties on the target object within the proxy through directly getting or setting the values with bracket notation. Instead, we can use the Reflect object. The methods on the Reflect object have the same name as the methods on the handler object.

Instead of accessing properties through obj[prop] or setting properties through obj[prop] = value, we can access or modify properties on the target object through Reflect.get() and Reflect.set(). The methods receive the same arguments as the methods on the handler object.

const personProxy = new Proxy(person, {
  get: (obj, prop) => {
    console.log(`The value of ${prop} is ${Reflect.get(obj, prop)}`);
  },
  set: (obj, prop, value) => {
    console.log(`Changed ${prop} from ${obj[prop]} to ${value}`);
    Reflect.set(obj, prop, value);
  },
});

Perfect! We can access and modify the properties on the target object easily with the Reflect object.

index.js
1const person = {
2 name: "John Doe",
3 age: 42,
4 nationality: "American"
5};
6
7const personProxy = new Proxy(person, {
8 get: (obj, prop) => {
9 console.log(`The value of ${prop} is ${Reflect.get(obj, prop)}`);
10 },
11 set: (obj, prop, value) => {
12 console.log(`Changed ${prop} from ${obj[prop]} to ${value}`);
13 return Reflect.set(obj, prop, value);
14 }
15});
16
17personProxy.name;
18personProxy.age = 43;
19personProxy.name = "Jane Doe";

Tradeoffs

Proxies are a powerful way to add control over the behavior of an object. A proxy can have various use-cases: it can help with validation, formatting, notifications, or debugging.

Overusing the Proxy object or performing heavy operations on each handler method invocation can easily affect the performance of your application negatively. It’s best to not use proxies for performance-critical code.


References