Proxy
In one line
A Proxy wraps an object and lets you intercept fundamental operations like get, set, and has via handler traps.
In simple words
A Proxy is an object that wraps another object (the target) and lets you customize fundamental operations on it — property access, assignment, deletion, enumeration, function calls — by defining handler functions called traps. Each trap corresponds to a Reflect method.
For example, a get trap runs every time any property is read on the proxy. You can log access, return computed values, validate assignments, or implement virtual properties that do not exist on the target. A set trap can validate or reject writes. The apply trap intercepts function calls.
Proxies power reactive systems (Vue 3's reactivity), validation libraries, and meta-programming. They are transparent: code interacts with the proxy as if it were the original object. Combine with Reflect to easily forward to the default behavior inside a trap.
Example
const handler = {
get(target, prop) {
console.log(`read ${String(prop)}`);
return Reflect.get(target, prop);
},
set(target, prop, value) {
if (prop === "age" && value < 0) throw new Error("invalid age");
return Reflect.set(target, prop, value);
},
};
const person = new Proxy({ name: "Ada", age: 36 }, handler);
console.log(person.name); // logs "read name", then "Ada"
person.age = 40; // ok
// person.age = -1; // throwsThe get trap logs every property read. The set trap validates age. Reflect forwards to the default behavior.
Related terms
Related Resources
Related Lessons
- Advanced Objects
Learn Proxy and Reflect.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.