Validation
Reject or transform invalid assignments before they reach the target.
JS Puzzles · Const
JavaScript Proxy traps let a handler intercept fundamental operations on a target, such as property access, assignment, deletion, calls, and construction. Traps can customize behavior or delegate transparently to the target.
A proxy handler can intercept fundamental operations on a target object through named traps. Each trap receives operation-specific arguments and can implement custom behavior or delegate to Reflect.
new Proxy(target, handler)| Name | Type | Description |
|---|---|---|
target | object or function | The original object or function whose operations may be intercepted. |
handler | object | An object containing trap methods for selected operations. |
property | string or symbol | The property key involved in a property operation. |
receiver | object | The object used as the receiver for get and set operations. |
A Proxy object that forwards unhandled operations to the target and applies the handler's traps where defined.
const target = { name: "Ada", _secret: 42 };
const proxy = new Proxy(target, {
get(target, property, receiver) {
if (property === "_secret") throw new Error("private property");
return Reflect.get(target, property, receiver);
},
set(target, property, value, receiver) {
if (property === "name" && typeof value !== "string") return false;
return Reflect.set(target, property, value, receiver);
},
ownKeys(target) {
return Reflect.ownKeys(target).filter((key) => !String(key).startsWith("_"));
}
});
console.log(proxy.name);
proxy.name = "Grace";
console.log(Object.keys(proxy));The proxy validates assignments, hides a private-looking property, and filters it from key enumeration.
Reject or transform invalid assignments before they reach the target.
Record reads, writes, calls, or other operations for diagnostics.
Expose a controlled interface while hiding selected properties or operations.
Add behavior to callable or constructible objects without changing their original implementation.
A proxy wraps a target and uses a handler to intercept operations such as property reads, writes, deletion, calls, and construction. The handler can handle an operation itself or forward it to the target, commonly through Reflect.
Follow-up: How can a proxy intercept a function call? Define an apply trap with signature apply(target, thisArg, args), or use a construct trap for new.
The get trap runs when a property is read and receives target, property, and receiver. The set trap runs when a property is assigned and receives target, property, value, and receiver; it should return a boolean indicating success.
Follow-up: Why pass receiver to Reflect.get or Reflect.set? It preserves the proxy as the receiver, which matters for inherited accessors and prototype-based behavior.
A proxy cannot freely contradict certain target invariants. For example, a set trap cannot report success while changing a non-writable, non-configurable data property to a different value; violations can produce TypeError.
Follow-up: What other trap can affect enumeration? ownKeys controls keys returned by operations such as Object.keys, while descriptor and configurability rules still constrain the result.
Reflecthandler.gethandler.sethandler.hashandler.deletePropertyhandler.ownKeyshandler.applyhandler.construct