JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Proxy & Reflect

javascript
senior
metaprogramming

What are Proxies in JavaScript and how can they be used?

proxy
metaprogramming
reflect
validation
reactive
Quick Answer

A Proxy wraps an object and intercepts fundamental operations (get, set, delete, etc.) through handler traps. They enable metaprogramming patterns like validation, logging, lazy loading, and implementing reactive systems.

Detailed Explanation

Proxy Basics:

  • new Proxy(target, handler) creates a proxy
  • target: The original object to wrap
  • handler: Object with trap methods

Common Traps:

  • get(target, prop, receiver): Property access
  • set(target, prop, value, receiver): Property assignment
  • has(target, prop): 'in' operator
  • deleteProperty(target, prop): delete operator
  • apply(target, thisArg, args): Function calls
  • construct(target, args): new operator

Use Cases:

  1. Validation: Validate values before setting
  2. Logging/Debugging: Track property access
  3. Default values: Return defaults for missing properties
  4. Reactive systems: Trigger updates on changes
  5. Access control: Restrict property access

Code Examples

Basic Proxy with validationJavaScript
const validator = {
  set(target, prop, value) {
    if (prop === 'age') {
      if (!Number.isInteger(value)) {
        throw new TypeError('Age must be an integer');
      }
      if (value < 0 || value > 150) {
        throw new RangeError('Age must be between 0 and 150');
      }
    }
    target[prop] = value;
    return true;
  }
};

const person = new Proxy({}, validator);
person.name = 'Alice'; // OK
person.age = 30;       // OK
person.age = -5;       // RangeError
person.age = 'thirty'; // TypeError

Real-World Applications

Use Cases

Reactive State Management

Wrapping state objects in Proxies that track property access and trigger UI updates on mutation, as used by Vue 3 and MobX

API Request Validation Layer

Creating validation proxy wrappers that enforce data shapes, required fields, and type coercion before data reaches business logic

Immutable State Updates

Using Proxies to enable mutable-style code that produces immutable results with structural sharing, as implemented by Immer

Mini Projects

Observable Store with Proxy

intermediate

Build a lightweight reactive store supporting property change subscriptions, computed properties, batched updates, and deep reactivity

Schema-Validated Data Models

advanced

Create a model system using Proxy where schemas automatically validate assignments, provide defaults, and log access patterns

Industry Examples

Vue.js

Vue 3 reactivity is built entirely on Proxies for automatic dependency tracking, deep reactivity, and property addition detection

Immer

Uses Proxy for draft states enabling mutable-looking code that produces immutable updates with structural sharing

MobX

Uses ES6 Proxies for observable objects with fine-grained reactivity tracking and automatic derivations

Resources

MDN - Proxy

docs

JavaScript.info - Proxy and Reflect

article

Related Questions

What are WeakMap and WeakSet, and how do they differ from Map and Set?

mid
weakrefs
Previous
What are Generators in JavaScript and when would you use them?
Next
How does JavaScript garbage collection work and how can you prevent memory leaks?
PrevNext