Using proxies
An alternative approach to implements data binding may be based on a new feature introduced by ECMAScript 6-proxies.
The proxy class
The proxy class allows us to create special objects that can change the default behavior when an object is accessed. When creating a proxy for an object, we can define a handler and configure traps in order to intercept accesses to its property and possibly change the standard behavior.
Let's explain the basic behavior of proxies with an example. Suppose we want to track to the console every access to the properties of an object. We can define the following handler:
var handler = { get (target, propertyName) { console.log("Getting property " + propertyName); return target[propertyName]; }, set(target, propertyName, ...
Get Mastering JavaScript Object-Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.