Our content is free thanks to ag-Grid

ag-Grid is the industry leading JavaScript datagrid

ag-grid.com

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.

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.

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

Get

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:

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:

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.

Set

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

Let’s validate the input of the age value:

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:

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.

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
Michael Karén

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
Michael Karén

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
20 January 20216 min read
Angular and SOLID principles

In software engineering, making things work the first time is always easy. But, what if you want to add new functionalities to an existing code? Making iterations on an existing basis can be difficult to do without introducing bugs. This is where SOLID principles come into play.

Angularpost
20 January 20216 min read
Angular and SOLID principles

In software engineering, making things work the first time is always easy. But, what if you want to add new functionalities to an existing code? Making iterations on an existing basis can be difficult to do without introducing bugs. This is where SOLID principles come into play.

Read more
AngularpostAngular and SOLID principles

20 January 2021

6 min read

In software engineering, making things work the first time is always easy. But, what if you want to add new functionalities to an existing code? Making iterations on an existing basis can be difficult to do without introducing bugs. This is where SOLID principles come into play.

Read more
Angularpost
14 January 20216 min read
Demystifying Taiga UI root component: portals pattern in Angular

Just before new year we announced our new Angular UI kit library Taiga UI. If you go through Getting started steps, you will see that you need to wrap your app with the tui-root component. Let's see what it does and explore what portals are and how and why we use them.