A Proxy wraps an object and lets you intercept what happens to it — reading a property, setting one, deleting one, even calling it as a function. Reflect is its companion: a set of methods that perform the default behaviour you would otherwise override. Together they are the foundation of reactivity in frameworks like Vue, and they are simpler than they sound.
You create a proxy with a target object and a handler that defines traps. The get trap runs on every property read, and set runs on every write.
const user = { name: "Arhan", age: 22 }; const watched = new Proxy(user, { get(target, property) { console.log(`Reading ${property}`); return target[property]; }, set(target, property, value) { console.log(`Setting ${property} to ${value}`); target[property] = value; return true; } }); watched.name; // logs "Reading name" watched.age = 23; // logs "Setting age to 23" The classic use case is validation: reject bad values at the moment they are assigned, instead of discovering them later. A set trap can check the value and return false — or throw — when it is invalid.
const safeUser = new Proxy(user, { set(target, property, value) { if (property === "age" && (typeof value !== "number" || value < 0)) { throw new Error("Age must be a positive number"); } return Reflect.set(target, property, value); } }); safeUser.age = -5; // throws Error: Age must be a positive number Reflect methods do the same thing a plain operation does, but as functions you can call — and they return success or failure instead of throwing. That is why handlers use Reflect.get and Reflect.set instead of touching the target directly: it keeps the default behaviour intact and avoids subtle bugs.
// Same as target[property], but as a safe function call Reflect.get(user, "name"); // "Arhan" Reflect.set(user, "age", 23); // true A word of caution: proxies add overhead to every operation on the object, so do not wrap everything. Use them where interception earns its keep — validation, logging, reactivity, or default values for missing properties.
Proxy and Reflect are advanced tools, but the pattern is small: a handler, a few traps, and Reflect for the defaults. Learn them once, and you will understand how modern frameworks make plain objects reactive.
