JavaScript Proxy: Intercept and Control Object Operations


Overview

JavaScript has a built-in Proxy object that lets you create a wrapper around any object or function. This wrapper intercepts and customizes operations performed on the target — reading properties, writing values, deleting keys, calling functions, and more.

Caller → Proxy → Real Object

The proxy decides what happens before the real object is touched.


Table of Contents

  1. What Is a Proxy?
  2. Basic Syntax
  3. Intercepting Property Access — get Trap
  4. Validating Writes — set Trap
  5. Default Values for Missing Properties
  6. Logging Every Change
  7. Access Control
  8. Intercepting Function Calls — apply Trap
  9. Common Proxy Traps
  10. Real-World Usage
  11. When to Use and When to Avoid
  12. The Proxy Pattern Beyond JavaScript
  13. Summary

What Is a Proxy?

↑ Index

new Proxy() creates an object that intercepts and customizes operations performed on another object or function.

In simple terms: a Proxy lets you control what happens when someone reads, writes, deletes, or calls something on an object.

It is commonly used for:

  • Validation — enforce rules before values are set
  • Logging — track every property access or change
  • Access control — hide or protect sensitive properties
  • Reactivity systems — automatically update the UI when data changes (Vue.js)
  • Meta-programming — customize fundamental object behavior

Basic Syntax

↑ Index

const proxy = new Proxy(target, handler);
ParameterDescription
targetThe original object you want to wrap
handlerAn object that defines traps — functions that intercept specific operations
const target = {};
const handler = {};

const proxy = new Proxy(target, handler);

With an empty handler, the proxy behaves identically to the target — all operations pass through unchanged. Traps are what make it useful.


Intercepting Property Access — get Trap

↑ Index

The get trap runs every time a property is read on the proxy.

const user = { name: "John" };

const handler = {
  get(target, property) {
    console.log(`Accessing ${property}`);
    return target[property];
  },
};

const proxyUser = new Proxy(user, handler);

console.log(proxyUser.name);

Output:

Accessing name
John

What happened:

  1. proxyUser.name is accessed
  2. The proxy intercepts it with the get trap
  3. Logs the access
  4. Returns the original value from the target

The caller interacts with proxyUser as if it were the real user object — but every read passes through the get trap first.


Validating Writes — set Trap

↑ Index

The set trap runs every time a property is written. You can enforce rules before the value reaches the real object.

const person = { age: 20 };

const proxy = new Proxy(person, {
  set(target, property, value) {
    if (property === "age" && value < 0) {
      throw new Error("Age cannot be negative");
    }
    target[property] = value;
    return true;
  },
});

proxy.age = 25; // ✅ works
proxy.age = -5; // ❌ throws Error: "Age cannot be negative"

The set trap receives three arguments:

ArgumentDescription
targetThe original object
propertyThe property name being set
valueThe new value being assigned

Returning true signals that the assignment succeeded. Throwing an error prevents the assignment entirely.


Default Values for Missing Properties

↑ Index

You can use the get trap to return default values instead of undefined when a property doesn't exist.

const handler = {
  get(target, prop) {
    if (prop in target) {
      return target[prop];
    }
    return "Not found";
  },
};

const proxy = new Proxy({}, handler);

console.log(proxy.name); // "Not found"
console.log(proxy.age);  // "Not found"

Instead of getting undefined for missing properties, the proxy returns "Not found".


Logging Every Change

↑ Index

The set trap makes it easy to log every property change — useful for debugging or building audit trails.

const handler = {
  set(target, prop, value) {
    console.log(`Property ${prop} changed to ${value}`);
    target[prop] = value;
    return true;
  },
};

const state = new Proxy({}, handler);

state.count = 1;
state.count = 2;

Output:

Property count changed to 1
Property count changed to 2

Every assignment is logged automatically — the calling code doesn't need to know it's being observed.


Access Control

↑ Index

You can use a proxy to protect sensitive properties — blocking access to certain fields while allowing others.

const user = { name: "Admin", password: "secret123" };

const proxy = new Proxy(user, {
  get(target, prop) {
    if (prop === "password") {
      return "Access denied";
    }
    return target[prop];