Ecmascript 6 - Proxies at a Glance

Nov 7th, 2012

The next version of ECMAScript, or “Harmony”, introduces Proxies. What is a proxy, you ask? Merriam-webster defines it as “authority or power to act for another”. And that’s simply what a Proxy object allows you to do in JavaScript: act on behalf of another object. In other words, a Proxy object can be used to intercept calls, or property access, from some other object. Use cases may not be apparent at first so consider the following: Logging when we get/set a property on an object, fire events when getting/setting a property on an object, transparent data binding on an object. In this post I’ll explore the first and the last.

Logging Access

Let’s imagine you would like to create an audit log of when an object was changed. Maybe you’re trying to track down a bug, or maybe you want an audit trail of modifications. One solution is to use myObject.set('prop', 'value'); or myObject.get('prop'). This solution is pretty simple! However, it requires boiler-plate code and extra typing. It would be nice if we could use regular js accessor properties right? Or, if we’re tracing down a bug we may end up littering our code with console.log statements. We can alleviate these problems by using a Proxy.

The code above will proxy the get and set mutators to the Proxy object created in loggable. It starts by defining a function that creates a Proxy for some object obj and using some logger logger. Next, we’ve defined the action required for the object’s get and set mutators. In get we’re simply logging that a property was accessed. In set we’re logging what we’re updating the object to as well as what it was in its old state. The debugger; statement is a testament to how easy debugging becomes. We create a person object and then assign it to a Proxy object. Finally, we execute basic get and set mutators to see our logging statements appear in the console.

I’ve ran this code in Firefox and Chrome (with ‘Enable Experimental JavaScript’ set to enabled in about://flags).

Data Binding

Data binding has caught on in the recent explosion of JavaScript frameworks such as AngularJS, Ember.js, Knockout, etc. Data binding allows us to update our JavaScript object and instantly see the change reflected on our page. Some frameworks, like Ember.js, make you use the .get() and .set() notation which, as previously stated, is extra typing and doesn’t use the native js mutators. On the other hand, frameworks like Angular and Knockout allow you to use native mutators but must constantly monitor the state of every object with some sort of equality check performed at a regular interval.

The Proxy object gives us the best of both worlds. We can use native mutators and we don’t need any monitoring. Here’s an very naive example.

If you made it this far, I won’t bother you with the details of what’s going on but give a high level overview. We look at our html for any tags that have the data-model attribute. Next, we loop over every match and perform a breadth-first search for text nodes that contain our special template syntax of {{<nameOfAttributeOnModel>}}. Once we’ve built an inverted index of template matchings to nodes that need updating, we create a Proxy of the data model, passing it our index so that anytime a property is changed on our Proxy we can instantly find what nodes need updating to reflect in our DOM. Finally, to show data binding working, we update the age of our test object every second which is reflected in the DOM; no verbose syntax or object equality checking required.

Conclusion

Proxies help provide many conveniences but comes with its share of problems. For example, your proxy will also trap calls to functions. This means you’ll have to run a typeof on your property to see if it’s a function. Fortunately the Proxy specification is still very much a working draft. Perhaps it will get a call trap for trapping function invocations. I wouldn’t recommend the use of proxies in production code. However, for debugging code I’d highly recommend it.