# Proxy & Reflect

> Master JavaScript metaprogramming. Intercept and redefine fundamental object operations like reading, writing, and deleting properties.

Subject: JavaScript | Lesson 50 of 63 | 7 min read
Interactive lesson: https://codewithanimation.in/javascript/proxy-and-reflect
Previous: [Generators & Iterators](https://codewithanimation.in/javascript/generators-and-iterators.md) | Next: [Error Handling: try/catch](https://codewithanimation.in/javascript/error-handling.md)

## The Interceptor

In JavaScript, interacting with objects is usually direct. If you write `user.age = 25`, the property is instantly updated.

A **Proxy** acts as a wrapper around a target object. It intercepts fundamental operations (reading, writing, deleting) and allows you to run custom code *before* the operation completes. Think of it as a security checkpoint.

## Validation and Reactivity

Why intercept operations?

1. **Validation**: You can prevent someone from setting an invalid age (e.g., `user.age = -5`).

2. **Reactivity**: Frameworks like Vue 3 use Proxies to detect exactly when a property changes so they can automatically trigger UI re-renders.

3. **Logging**: You can log every time a specific property is read or written to.

## Traps and Reflect

To create a Proxy, you provide a `target` object and a `handler` object containing 'traps' (interceptor functions).

```javascript
const handler = {
  set(target, prop, value) {
    if (prop === 'age' && value < 0) throw new Error('Invalid age');
    return Reflect.set(target, prop, value);
  }
};
const proxy = new Proxy({}, handler);
```

**Reflect** is a built-in object that pairs perfectly with Proxy. After you run your custom logic, you use `Reflect.set` to actually apply the operation to the target object securely.

## Common mistake: The Performance Cost

Metaprogramming comes with a cost. Because every property access on a Proxy must run through your trap functions, Proxies are noticeably slower than direct object access.

Avoid wrapping massive arrays or highly volatile objects in a Proxy unless absolutely necessary for your architecture.

## Tip: Revocable Proxies

If you want to grant temporary access to an object and then completely cut it off later, you can use `Proxy.revocable()`. Calling `.revoke()` instantly permanently disables the proxy, throwing an error on any future access!

## Try it: The Security Checkpoint

Study the architecture diagram below. It maps out the exact flow of data when a write operation (`user.age = -5`) hits the Proxy's `set` trap and is subsequently rejected by the validation logic.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/proxy-and-reflect

## Check yourself

### Question 1

What is the purpose of the 'handler' object when creating a Proxy?

- A. It contains 'trap' functions (like set and get) that intercept operations.
- B. It stores the raw data of the object.
- C. It connects the Proxy to a database.

Correct answer: A

The handler defines the custom behavior of the proxy. If a trap isn't defined in the handler, the operation falls back to standard behavior.

- Why not B: Incorrect. The `target` object stores the raw data.
- Why not C: Incorrect. Proxies operate entirely in memory.

### Question 2

Why is `Reflect.set()` typically called at the end of a Proxy's `set` trap?

- A. To actually apply the value to the original target object after custom logic completes.
- B. To trigger a UI re-render.
- C. To convert the value into a string.

Correct answer: A

`Reflect` guarantees that the default behavior of the operation executes correctly and returns the proper boolean success value required by the Proxy.

- Why not B: Incorrect. `Reflect` is a core JS feature, not a UI framework feature.
- Why not C: Incorrect. `Reflect.set` forwards the assignment operation.

### Question 3

Which framework famously rebuilt its entire reactivity system using ES6 Proxies?

- A. Vue (Version 3)
- B. React
- C. Angular

Correct answer: A

Vue 3 replaced `Object.defineProperty` with ES6 Proxies, allowing it to seamlessly track property additions and deletions without explicitly declaring them first.

- Why not B: Incorrect. React uses state hooks and Virtual DOM diffing, not Proxies.
- Why not C: Incorrect. Angular uses Zone.js for change detection.

## Remember this

- A **Proxy** wraps an object and intercepts fundamental operations like reading or writing properties.
- The **handler** object contains 'traps' (e.g., `get`, `set`) that define the interception logic.
- The **Reflect** API provides standard methods (e.g., `Reflect.set()`) to execute the default behavior safely.
- Proxies are powerful for data validation, logging, and building reactive UI frameworks.
- Proxies carry a performance overhead compared to direct object access.
