Proxy wraps an object and intercepts fundamental operations — property access, assignment, deletion, function calls, and more. Reflect provides the default behavior for each trap.
Think of a Proxy like a personal assistant. When someone tries to ask you a question (read a property), your assistant intercepts it first. The assistant can: answer directly, modify the response, log the request, or block it entirely. The "real you" (the target object) doesn't even know someone asked. Together Proxy + Reflect enable validation, observables, negative array indices, and more.
Prerequisites: JS Foundations #4 — Prototypes, Symbols
1. Proxy Basics — Intercepting Property Access
A Proxy takes a target object and a handler with traps — functions that intercept operations:
Loading editor...
2. Why Reflect? — Not Just Convenience
Every Proxy trap has a matching Reflect method. You could write target[prop] instead of Reflect.get(target, prop), but Reflect is better for three reasons:
- Correct
thisfor getters:Reflect.get(target, prop, receiver)passes thereceiver(the proxy) asthiswhen the property is a getter.target[prop]would usetargetasthis, breaking getter behavior. - Consistent return values:
Reflect.set()returnstrue/false(like strict-mode assignment).target[prop] = valuejust returns the value. - No try/catch needed:
Reflect.defineProperty()returnsfalseon failure instead of throwing.
Loading editor...
3. Validation Proxy — Enforce Rules on Assignment
Loading editor...
4. Negative Array Index Proxy
Python-like negative indices accessing from the end:
Loading editor...
5. Observable Object — Track Changes
Implement a basic reactive store that notifies subscribers on changes:
Loading editor...
6. All 13 Proxy Traps
Loading editor...
7. Revocable Proxy
A proxy you can disable at any time — useful for access control that expires:
Loading editor...
8. Proxy Limitations — What CAN'T Be Intercepted
Not everything goes through traps. These operations bypass the Proxy entirely:
Loading editor...
Proxies are for specific use cases (validation, observability, logging), not for blanket wrapping of all your data.
Key Takeaways
-
Proxy(target, handler)wraps an object and intercepts 13 fundamental operations via traps. -
Reflectprovides the default behavior — always useReflectfor correct getterthis, consistent returns, and no-throw semantics. -
The
receiverparameter in traps is the proxy itself — critical for correct getter behavior on prototype chains. -
Use cases: validation, negative indices, observables, lazy loading, access logging.
-
Proxy.revocable()creates a proxy you can later disable completely. -
Proxies can't intercept
typeof,===, or directtargetaccess — they wrap, they don't replace. -
Proxy(target, handler)wraps an object and intercepts 13 fundamental operations via traps. -
Reflectprovides the default behavior — always useReflectinstead of directtarget[prop]in traps. -
Use cases: validation, negative indices, observables, lazy loading, access logging.
-
Proxy.revocable()creates a proxy you can later disable completely. -
Proxies are transparent —
proxy !== targetbutproxy instanceof Targetworks.
Next: Parsing #1 — Building a Simple JSON.stringify() — understand the JSON grammar by implementing stringify.
