Dirk Harriman Banner Image

 

Notes Javascript - Meta Programming

Sub Menu Here

 

 

Meta Programming


The Proxy and Reflect objects allow you to intercept and define custom behavior for fundamental language operations (e.g. property lookup, assignment, enumeration, function invocation, etc.). With the help of these two objects you are able to program at the meta level of JavaScript.

Proxy

The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for that object.
Proxy objects allow you to intercept certain operations and to implement custom behaviors.
For example, getting a property on an object:

const handler = { get(target, name) { return name in target ? target[name] : 42; }, }; const p = new Proxy({}, handler); p.a = 1; console.log(p.a, p.b); // 1, 42

The Proxy object defines a target (an empty object here) and a handler object, in which a get trap is implemented. Here, an object that is proxied will not return undefined when getting undefined properties, but will instead return the number 42.

Description

The Proxy object allows you to create an object that can be used in place of the original object, but which may redefine fundamental Object operations like getting, setting, and defining properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on.

You create a Proxy with two parameters:

For example, this code creates a proxy for the target object:

const target = { message1: "hello", message2: "everyone", }; const handler1 = {}; const proxy1 = new Proxy(target, handler1);

Because the handler is empty, this proxy behaves just like the original target:

console.log(proxy1.message1); // hello console.log(proxy1.message2); // everyone

To customize the proxy, we define functions on the handler object:

const target = { message1: "hello", message2: "everyone", }; const handler2 = { get(target, prop, receiver) { return "world"; }, }; const proxy2 = new Proxy(target, handler2);

Here we've provided an implementation of the get() handler, which intercepts attempts to access properties in the target.

Handler functions are sometimes called traps, presumably because they trap calls to the target object. The very simple trap in handler2 above redefines all property accessors:

console.log(proxy2.message1); // world console.log(proxy2.message2); // world

Proxies are often used with the Reflect object, which provides some methods with the same names as the Proxy traps. The Reflect methods provide the reflective semantics for invoking the corresponding object internal methods. For example, we can call Reflect.get if we don't wish to redefine the object's behavior:

const target = { message1: "hello", message2: "everyone", }; const handler3 = { get(target, prop, receiver) { if (prop === "message2") { return "world"; } return Reflect.get(...arguments); }, }; const proxy3 = new Proxy(target, handler3); console.log(proxy3.message1); // hello console.log(proxy3.message2); // world

The Reflect method still interacts with the object through object internal methods, it doesn't "de-proxify" the proxy if it's invoked on a proxy. If you use Reflect methods within a proxy trap, and the Reflect method call gets intercepted by the trap again, there may be infinite recursion.

Terminology

The following terms are used when talking about the functionality of proxies.

handler Placeholder object which contains traps.
traps The methods that provide property access. (This is analogous to the concept of traps in operating systems.)
target Object which the proxy virtualizes. It is often used as storage backend for the proxy. Invariants (semantics that remain unchanged) regarding object non-extensibility or non-configurable properties are verified against the target.
invariants Semantics that remain unchanged when implementing custom operations are called invariants. If you violate the invariants of a handler, a TypeError will be thrown.