Proxy & Reflect — Validation, Observables & Traps

Build a validation proxy, a negative-array-index proxy, an observable object, and understand all 13 proxy traps with Reflect counterparts.

12 min read
JavaScript
ES6
Proxy
Meta

TABLE OF CONTENTS
Proxy & Reflect — Validation, Observables & Traps

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:

  1. Correct this for getters: Reflect.get(target, prop, receiver) passes the receiver (the proxy) as this when the property is a getter. target[prop] would use target as this, breaking getter behavior.
  2. Consistent return values: Reflect.set() returns true/false (like strict-mode assignment). target[prop] = value just returns the value.
  3. No try/catch needed: Reflect.defineProperty() returns false on 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.

  • Reflect provides the default behavior — always use Reflect for correct getter this, consistent returns, and no-throw semantics.

  • The receiver parameter 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 direct target access — they wrap, they don't replace.

  • Proxy(target, handler) wraps an object and intercepts 13 fundamental operations via traps.

  • Reflect provides the default behavior — always use Reflect instead of direct target[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 transparentproxy !== target but proxy instanceof Target works.


Next: Parsing #1 — Building a Simple JSON.stringify() — understand the JSON grammar by implementing stringify.


Let's Connect

© 2026 Naveen Karthik // Built with React & MUI