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, value) { console.log("Assigning value " + value + " to property " + propertyName); target[propertyName] = value; } };
This handler is an object with two methods, get()
and set()
, that intercept the...