JS Proxy Pattern
Intercept and control object property access with ES6 Proxy
When to Use
- You need to validate, log, or transform property reads/writes without changing the target object
- Implementing reactive data systems (watching object mutations)
- Adding access control or lazy initialization to expensive objects
- Building observable models for state management
Instructions
- Create a handler object with trap methods (
get, set, deleteProperty, etc.).
- Wrap the target object:
const proxy = new Proxy(target, handler).
- In
set traps, always call Reflect.set(target, prop, value) to apply the change and return its boolean result.
- In
get traps, use Reflect.get(target, prop, receiver) to preserve prototype chain behavior.
- Use
Reflect methods in traps — they mirror the Proxy trap API and ensure correct semantics.
const validator = {
set(target, prop, value) {
if (prop === 'age') {
if (typeof value !== 'number' || value < 0) {
throw new TypeError('Age must be a non-negative number');
}
}
return Reflect.set(target, prop, value);
},
};
const person = new Proxy({}, validator);
person.age = 30; // OK
person.age = -1; // Throws TypeError
- Avoid deeply nested Proxy wrapping — it compounds performance overhead on every property access.
Details
ES6 Proxy gives you a meta-programming hook at the object level. Traps intercept fundamental operations: get, set, has, deleteProperty, apply (for functions), and construct (for classes).
Trade-offs:
- Proxy adds overhead per property access — avoid on hot paths (tight loops, rendering cycles)
- Proxied objects are not equal to their targets (
proxy !== target) — equality checks must use the target
- Proxies are not serializable —
JSON.stringify(proxy) serializes the underlying target, which may surprise callers
- Debugging is harder — the DevTools shows the proxy wrapper, not the target directly
When NOT to use:
- For simple validation — just write a setter method or use a class
- For immutability at scale —
Object.freeze() is simpler and has no runtime overhead per access
- When you need ES5 compatibility — Proxy cannot be polyfilled
Related patterns:
- Observer Pattern — Proxy can power reactive observation of object mutations
- Singleton Pattern — a Proxy can wrap a singleton to control access
Source
https://patterns.dev/javascript/proxy-pattern
Process
- Read the instructions and examples in this document.
- Apply the patterns to your implementation, adapting to your specific context.
- Verify your implementation against the details and edge cases listed above.
Harness Integration
- Type: knowledge — this skill is a reference document, not a procedural workflow.
- No tools or state — consumed as context by other skills and agents.
Success Criteria
- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.
1---2name: js-proxy-pattern3description: JS Proxy Pattern4metadata:5 source: https://skillmd.com/skills/intense-visions/js-proxy-pattern6---7# JS Proxy Pattern89> Intercept and control object property access with ES6 Proxy1011## When to Use1213- You need to validate, log, or transform property reads/writes without changing the target object14- Implementing reactive data systems (watching object mutations)15- Adding access control or lazy initialization to expensive objects16- Building observable models for state management1718## Instructions19201. Create a handler object with trap methods (`get`, `set`, `deleteProperty`, etc.).212. Wrap the target object: `const proxy = new Proxy(target, handler)`.223. In `set` traps, always call `Reflect.set(target, prop, value)` to apply the change and return its boolean result.234. In `get` traps, use `Reflect.get(target, prop, receiver)` to preserve prototype chain behavior.245. Use `Reflect` methods in traps — they mirror the Proxy trap API and ensure correct semantics.2526```javascript27const validator = {28 set(target, prop, value) {29 if (prop === 'age') {30 if (typeof value !== 'number' || value < 0) {31 throw new TypeError('Age must be a non-negative number');32 }33 }34 return Reflect.set(target, prop, value);35 },36};3738const person = new Proxy({}, validator);39person.age = 30; // OK40person.age = -1; // Throws TypeError41```42436. Avoid deeply nested Proxy wrapping — it compounds performance overhead on every property access.4445## Details4647ES6 `Proxy` gives you a meta-programming hook at the object level. Traps intercept fundamental operations: get, set, has, deleteProperty, apply (for functions), and construct (for classes).4849**Trade-offs:**5051- Proxy adds overhead per property access — avoid on hot paths (tight loops, rendering cycles)52- Proxied objects are not equal to their targets (`proxy !== target`) — equality checks must use the target53- Proxies are not serializable — `JSON.stringify(proxy)` serializes the underlying target, which may surprise callers54- Debugging is harder — the DevTools shows the proxy wrapper, not the target directly5556**When NOT to use:**5758- For simple validation — just write a setter method or use a class59- For immutability at scale — `Object.freeze()` is simpler and has no runtime overhead per access60- When you need ES5 compatibility — Proxy cannot be polyfilled6162**Related patterns:**6364- Observer Pattern — Proxy can power reactive observation of object mutations65- Singleton Pattern — a Proxy can wrap a singleton to control access6667## Source6869https://patterns.dev/javascript/proxy-pattern7071## Process72731. Read the instructions and examples in this document.742. Apply the patterns to your implementation, adapting to your specific context.753. Verify your implementation against the details and edge cases listed above.7677## Harness Integration7879- **Type:** knowledge — this skill is a reference document, not a procedural workflow.80- **No tools or state** — consumed as context by other skills and agents.8182## Success Criteria8384- The patterns described in this document are applied correctly in the implementation.85- Edge cases and anti-patterns listed in this document are avoided.