Sub topic 2
Reflect: The Default Behavior for Every Trap
This chapter assumes you already know how to create a proxy with a get or set trap. You should also be comfortable with prototypes and with how this binding works.
Reflect Mirrors Every Trap
Each proxy trap has a matching Reflect method with the same name and the same arguments: Reflect.get, Reflect.set, Reflect.has, Reflect.apply, and so on. Calling one of these methods runs the default internal operation. That makes Reflect the standard way to say "now do what would normally happen."
Reflect methods also report failure by returning false, where the matching Object methods throw. Traps such as set and defineProperty are expected to return a boolean, so you can pass the Reflect result straight back.
const obj = Object.preventExtensions({});
console.log(Reflect.defineProperty(obj, 'x', { value: 1 })); // false
try {
Object.defineProperty(obj, 'x', { value: 1 });
} catch (err) {
console.log(err instanceof TypeError); // true
}Forwarding Correctly: The Receiver
The receiver argument is the object the property access actually started on. If you write target[prop] inside a trap, any getter on the target runs with this set to the target. Passing receiver to Reflect.get keeps inheritance working:
const user = {
_name: 'Ada',
get name() { return this._name; }
};
const proxy = new Proxy(user, {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver);
}
});
const admin = Object.create(proxy);
admin._name = 'Grace';
console.log(admin.name); // 'Grace' (target[prop] would return 'Ada')Controlling in, delete, and Key Enumeration
The has trap intercepts the in operator. The deleteProperty trap intercepts delete. The ownKeys trap intercepts Object.keys, for...in, and Reflect.ownKeys.
const isHidden = (key) => typeof key === 'string' && key.startsWith('_');
const record = new Proxy({ _secret: 1, visible: 2 }, {
has(target, prop) {
return isHidden(prop) ? false : Reflect.has(target, prop);
},
deleteProperty(target, prop) {
return isHidden(prop) ? false : Reflect.deleteProperty(target, prop);
},
ownKeys(target) {
return Reflect.ownKeys(target).filter((key) => !isHidden(key));
}
});
console.log('_secret' in record); // false
console.log(Object.keys(record)); // ['visible']Proxying Functions with apply and construct
When the target is a function, the apply trap intercepts calls and the construct trap intercepts new.
function sum(a, b) {
return a + b;
}
const traced = new Proxy(sum, {
apply(target, thisArg, args) {
console.log(`called with ${args.join(', ')}`);
return Reflect.apply(target, thisArg, args);
},
construct(target, args, newTarget) {
console.log('constructed');
return Reflect.construct(target, args, newTarget);
}
});
console.log(traced(1, 2)); // logs "called with 1, 2", then 3
new traced(1, 2); // logs "constructed"Exercise
A get trap returns target[prop] instead of Reflect.get(target, prop, receiver). An object created with Object.create(proxy) reads an inherited getter that uses this. What goes wrong?
Show answer
The getter runs with this set to the original target instead of the inheriting object, so it reads the target's own data instead of the child object's data. Passing receiver to Reflect.get fixes this.