DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Proxy and Reflect in JavaScript: Metaprogramming Patterns
XLinkedInReddit
MediumFrontend Engineering

Proxy and Reflect in JavaScript: Metaprogramming Patterns

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • What is a Proxy?
  • Why Reflect?
  • Practical Patterns
  • 1. Validation Layer
  • 2. Negative Array Indexing
  • 3. Observable Objects (Vue 3 Reactivity)
  • Performance Considerations

Proxy is JavaScript's metaprogramming primitive. At Google, we use it for validation layers, reactive systems, and API mocking.

What is a Proxy?

A Proxy wraps an object and intercepts operations like property access, assignment, deletion, and function calls.

const handler = {
  get(target, prop, receiver) {
    console.log(`Accessing ${prop}`);
    return Reflect.get(target, prop, receiver);
  },
  set(target, prop, value, receiver) {
    console.log(`Setting ${prop} = ${value}`);
    return Reflect.set(target, prop, value, receiver);
  }
};
const user = new Proxy({}, handler);
user.name = "Rahul"; // logs: Setting name = Rahul
console.log(user.name); // logs: Accessing name

Why Reflect?

Reflect provides the default behavior for each trap. Always use Reflect inside proxy traps to maintain correct behavior with inheritance and receivers.

Practical Patterns

1. Validation Layer

const validated = new Proxy({}, {
  set(target, prop, value) {
    if (prop === "age" && (typeof value !== "number" || value < 0)) {
      throw new TypeError("Age must be a positive number");
    }
    return Reflect.set(target, prop, value);
  }
});

2. Negative Array Indexing

function negativeArray(arr) {
  return new Proxy(arr, {
    get(target, prop) {
      const index = Number(prop);
      if (index < 0) return target[target.length + index];
      return Reflect.get(target, prop);
    }
  });
}
const arr = negativeArray([1, 2, 3]);
arr[-1]; // 3

3. Observable Objects (Vue 3 Reactivity)

Vue 3's reactivity system is built entirely on Proxy. Every reactive object is a Proxy that tracks which properties are accessed during rendering and triggers re-renders on changes.

Performance Considerations

Proxies add overhead to every operation. Don't use them in hot paths. At Google, we use Proxies in development (for validation/debugging) and strip them in production builds.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What is a Proxy?
  • Why Reflect?
  • Practical Patterns
  • 1. Validation Layer
  • 2. Negative Array Indexing
  • 3. Observable Objects (Vue 3 Reactivity)
  • Performance Considerations

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.