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
- What Is a Proxy?
- Basic Syntax
- Intercepting Property Access —
getTrap - Validating Writes —
setTrap - Default Values for Missing Properties
- Logging Every Change
- Access Control
- Intercepting Function Calls —
applyTrap - Common Proxy Traps
- Real-World Usage
- When to Use and When to Avoid
- The Proxy Pattern Beyond JavaScript
- Summary
What Is a Proxy?
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
const proxy = new Proxy(target, handler);
| Parameter | Description |
|---|---|
target | The original object you want to wrap |
handler | An 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
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:
proxyUser.nameis accessed- The proxy intercepts it with the
gettrap - Logs the access
- 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
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:
| Argument | Description |
|---|---|
target | The original object |
property | The property name being set |
value | The new value being assigned |
Returning true signals that the assignment succeeded. Throwing an error prevents the assignment entirely.
Default Values for Missing Properties
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
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
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];