Full answer
new Proxy(target, handler) creates an object that stands in front of target. When code reads, writes, deletes, enumerates, or calls through the proxy, the engine looks for a matching function in handler, called a trap. If there is no trap, the operation goes straight to the target.
Common traps are get, set, has (for in), deleteProperty, ownKeys, apply (for function calls), and construct. The Reflect object has a method of the same name for each trap, such as Reflect.get and Reflect.set. Using it inside a trap performs the normal operation with correct receiver handling, and its return value tells the set trap whether to report success. In strict mode, a set trap that returns false throws a TypeError.
Uses include validation before assignment, default values for missing keys, logging, access control, and observable state, which is the foundation of the reactivity systems in modern UI frameworks. Proxy.revocable creates a proxy that can later be disabled.
Limitations matter in a senior discussion:
proxy !== target, so identity checks andWeakMapkeys behave differently.- Built-in objects with internal slots, such as
Map,Date, and class instances with private fields, may throw when a method is called with the proxy asthis. - Every operation goes through a function call, which is slower than plain access in hot code.
- Traps must obey invariants. For example, a non-configurable, non-writable property must report its real value.
- Proxies cannot be fully polyfilled.
'use strict';
const validator = {
set(target, property, value, receiver) {
if (property === 'age' && (!Number.isInteger(value) || value < 0)) {
throw new RangeError('age must be a non-negative integer');
}
return Reflect.set(target, property, value, receiver);
},
get(target, property, receiver) {
return property in target ? Reflect.get(target, property, receiver) : `no ${String(property)}`;
},
has(target, property) {
return !property.startsWith('_') && property in target;
},
};
const person = new Proxy({ name: 'Leela', _secret: 'x' }, validator);
person.age = 30;
console.log(person.age, person.missing, 'age' in person, '_secret' in person);
try {
person.age = -5;
} catch (error) {
console.log(error.name, '-', error.message);
}
const calls = [];
const traced = new Proxy(function add(a, b) { return a + b; }, {
apply(target, thisArg, args) {
calls.push(args);
return Reflect.apply(target, thisArg, args);
},
});
console.log(traced(2, 3), calls);
class Counter {
#count = 0;
increment() {
return ++this.#count;
}
}
const counterProxy = new Proxy(new Counter(), {});
try {
counterProxy.increment();
} catch (error) {
console.log('private field through proxy:', error.name);
}
const fixedProxy = new Proxy(new Counter(), {
get(target, property) {
const value = Reflect.get(target, property, target);
return typeof value === 'function' ? value.bind(target) : value;
},
});
console.log(fixedProxy.increment());
const { proxy, revoke } = Proxy.revocable({ value: 1 }, {});
console.log(proxy.value);
revoke();
try {
proxy.value;
} catch (error) {
console.log('after revoke:', error.name);
}Run with Node.js v24.22.1 as js-40.js; two runs gave identical output. Output: 30 no missing true false RangeError - age must be a non-negative integer 5 [ [ 2, 3 ] ] private field through proxy: TypeError 1 1 after revoke: TypeError
Key points
- A Proxy intercepts operations through handler traps; missing traps forward to the target.
- Reflect methods mirror the traps and give default behavior.
- A set trap must return true for success; false throws in strict mode.
- Proxies change identity and can break internal slots and private fields.
- Useful for validation, defaults, logging, and reactivity; costs performance.
Common mistakes
- Forgetting to return true from the set trap and getting a TypeError in strict mode.
- Using target[property] instead of Reflect.get with the receiver, which breaks getters and inheritance.
- Wrapping a Map or Date in a proxy and calling its methods without binding them.
- Using a proxy in a hot loop without measuring the cost.
Follow-ups
- How would you build a simple reactive state object with a Proxy?
- What are proxy invariants, and what happens when a trap violates one?
- How does a Proxy differ from Object.defineProperty for observing changes?
Tips
- Name three traps and one real use for the set trap, such as validation.
- Mention the private-field limit; it shows hands-on experience.
- Explain that Reflect mirrors the traps and keeps behavior correct, instead of treating it as a separate topic.