PROXY PATTERNS
The Proxy API allows you to introduce some incredibly useful patterns into your code.
Tracking Property Access
The nature of get
, set
, and has
affords you total insight into when object properties are being accessed and inspected. If you provide a trapped proxy to an object throughout your application, you will be able to see exactly when and where this object is accessed:
const user = {
name: 'Jake'
};
const proxy = new Proxy(user, {
get(target, property, receiver) {
console.log('Getting ${property}');
return Reflect.get(…arguments);
},
set(target, property, value, receiver) {
console.log('Setting ${property}=${value}');
return Reflect.set(…arguments);
}
});
proxy.name; // Getting name
proxy.age = 27; // Setting age=27
Hidden Properties
The innards of proxies are totally hidden to remote code so it is very easy to conceal the existence of properties on the target object. For example:
const hiddenProperties...