Sub topic 6
Forwarding Correctly: The Receiver Argument
This chapter assumes you already know how to write get and set traps, how Reflect.get and Reflect.set mirror the default behavior, and how prototype-chain lookup and this binding in getters work.
Every get and set trap receives a final argument called receiver. It is easy to ignore, and code that ignores it usually works until an object inherits from the proxy or a property uses a getter or setter. This chapter explains what the receiver is and how to forward it correctly.
What the receiver is
The receiver is the object the operation was originally performed on. When you write proxy.name, the receiver is the proxy itself. When you write child.name and child inherits from the proxy, the lookup walks up the prototype chain, reaches the proxy, and calls its get trap with child as the receiver.
The receiver decides two things:
- In a read, it is the
thisvalue that an accessor getter runs with. - In a write, it is the
thisvalue for a setter, and it is the object that receives a new data property.
| Operation | Trap called on | `target` | `receiver` |
|---|---|---|---|
| `proxy.x` | proxy | wrapped object | proxy |
| `child.x` (child inherits from proxy) | proxy | wrapped object | child |
| `child.x = 1` (no own `x` on child) | proxy | wrapped object | child |
Reads: getters need the receiver
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')If the trap returned target[prop], the getter would run with this set to user and return 'Ada', so the inheritance would quietly stop working. Reflect.get(target, prop) with no third argument fails the same way, because the receiver defaults to the target.
Writes: the receiver decides where data lands
Writes have the same problem, and the result is harder to spot because nothing throws.
const shared = new Proxy({}, {
set(target, prop, value, receiver) {
target[prop] = value; // wrong: ignores receiver
return true;
}
});
const correct = new Proxy({}, {
set(target, prop, value, receiver) {
return Reflect.set(target, prop, value, receiver);
}
});
const a = Object.create(shared);
const b = Object.create(shared);
a.color = 'red';
console.log(b.color); // 'red' (leaked into the shared target)
const c = Object.create(correct);
const d = Object.create(correct);
c.color = 'red';
console.log(Object.hasOwn(c, 'color')); // true
console.log(d.color); // undefinedWith Reflect.set and the receiver, the new property is created on c, which is how assignment behaves on ordinary objects. Writing to target puts the value on the one object every inheritor shares.
The forwarding rule
Forward every trap with the matching Reflect method and pass all of the trap's arguments through in order:
const handler = {
get(target, prop, receiver) {
return Reflect.get(target, prop, receiver);
},
set(target, prop, value, receiver) {
return Reflect.set(target, prop, value, receiver);
}
};Sometimes you have to pass target as the receiver on purpose. Built-ins such as Map keep their data in internal slots, and their getters reject a proxy as this. A later chapter covers that case. Unless you have a specific reason like that, pass the receiver you were given.
Exercise
A trap is written as get(target, prop) { return Reflect.get(target, prop); }. The target has a getter full that returns this.first + ' ' + this.last. An object emp inherits from the proxy and has its own first and last. What does emp.full return, and how do you fix it?
Show answer
It returns the target's first and last names, not emp's. The receiver defaults to the target, so the getter runs with this set to the target. The fix is to accept the third parameter and forward it: get(target, prop, receiver) { return Reflect.get(target, prop, receiver); }.
Exercise
In a set trap, why is return Reflect.set(target, prop, value, receiver) better than target[prop] = value; return true; when objects inherit from the proxy?
Show answer
Reflect.set with the receiver creates or updates the property on the object that was actually assigned to, and it runs setters with the right this. It also returns false when the write fails, for example on a non-writable property. The manual version always writes to the shared target and always reports success.