Getting Started with Modern JavaScript — Proxy

Post Editor

JavaScript proxies were introduced in 2015 with ECMAScript 6. They allow us to intercept and override operations such as object property lookup and assignment. A Proxy object wraps another object and acts as a middleman.

3 min read
post

Getting Started with Modern JavaScript — Proxy

JavaScript proxies were introduced in 2015 with ECMAScript 6. They allow us to intercept and override operations such as object property lookup and assignment. A Proxy object wraps another object and acts as a middleman.

post
post
3 min read
3 min read

JavaScript proxies were introduced in 2015 with ECMAScript 6. They allow us to intercept and override operations such as object property lookup and assignment. A Proxy object wraps another object and acts as a middleman.

proxy.png

Syntax

A proxy is created using the new Proxy constructor with two required arguments: the target and the handler.

<>Copy
let proxy = new Proxy(target, handler);
  • target — The object we wrap.
  • handler — An object that defines the methods (also called traps) to control the behaviors of the target.

A Proxy creates an undetectable barrier around the target object that redirects all operations to the handler object. If we send in an empty handler, the proxy is just an empty wrapper around the original object.

<>Copy
let user = { name: 'Max', age: 42 }; let proxyUser = new Proxy(user, {}); console.log(user.name); // -> Max console.log(proxyUser.name); // -> Max

To give the proxy meaning, we need to add some functionality to the handler.

Traps

Whenever you interact with an object, you are calling an internal method. Proxies allow you to intercept the execution of a given internal method with traps.

So when we run user.name we are telling the JavaScript engine to call the internal [[GET]] method to retrieve the name property.

When we run proxyUser.name the get trap calls the get() function defined in the handler to execute before sending the call through to the original object.

proxyuser.png

GetLink to this section

The get() method has two required parameters:

  • target — Object passed to the proxy.
  • property — Name of the accessed property.

To customize the proxy, we define functions on the handler object. Here we define the get method to log the access:

<>Copy
const handler = { get(target, property) { console.log(`GET ${property}`); return target[property]; } };

To let the call through, we return target[property].

Now, if we create a proxy with this handler and try to access the original object, we log the call:

<>Copy
const proxyUser = new Proxy(user, handler); console.log(proxyUser.age); // -> GET age // -> 42

We can see that when we access a property of the user object via the proxyUser object, the get() method fires in the handler object.

SetLink to this section

The set trap controls behavior when a property of the target object is assigned.

Let’s validate the input of the age value:

<>Copy
const handler = { set(target, property, value) { if(property === 'age' && typeof(value) !== 'number') { throw new TypeError('Age is just a number.'); } target[property] = value; return true; } }

If we try to assign the wrong type to age, an error is thrown:

<>Copy
proxyUser.age = 'old'; // -> TypeError: Age is just a number.

In the line target[property] = value we set the age property of the user object.

The set() method should return a boolean value true to indicate that the assignment succeeded. If the JavaScript is run in strict mode, and a falsy value or nothing is returned, an error will be thrown.

<>Copy
Uncaught TypeError:set’ on proxy: trap returned falsish for property 'age'
In addition to intercepting reads and modifications to properties, Proxy can intercept a total of 13 operations.

Conclusion

We have learned how we can use a proxy to spy on objects. You should now be able to add behaviors to them by using trap methods in the handler object. We have only just dipped our toes into proxies with a couple of basic examples, but it’s enough to get started and inspired to explore the possibilities!

Discuss with community

Share

About the author

author_image

Angular by day. React by night. Full-stack when needed. I like blogging about web stuff. Co-organizer ngVikings.

author_image

About the author

Michael Karén

Angular by day. React by night. Full-stack when needed. I like blogging about web stuff. Co-organizer ngVikings.

About the author

author_image

Angular by day. React by night. Full-stack when needed. I like blogging about web stuff. Co-organizer ngVikings.

NxAngularCli
NxAngularCli
NxAngularCli

Featured articles

Angularpost
10 May 20219 min read
Angular Forms: reactive design patterns catalog

In this post, you'll find a set of design patterns for building Angular forms based on two pillars: separation of responsibilities and reactive programming to tackle the complexity of rich and complex Angular forms.

Angularpost
6 May 20216 min read
A journey into NgRx Selectors

This article dives deep into NgRx selectors and will help you understand what role that play in NgRx architecture and how they help decrease the complexity of a codebase