Sub topic 6
Forwarding Correctly: The Receiver Argument
This chapter assumes you already know how get and set traps work, how Reflect.get and Reflect.set supply default behavior, and how getters, setters, and prototype-chain lookup behave on ordinary objects. Its subject is the last parameter of those traps, receiver, and why your forwarding code should pass it along.
What the Receiver Is
Every property read or write has two objects involved:
- The holder is the object where the property is actually found. In a trap, this is usually
target. - The receiver is the object the operation started on. It becomes
thisif the property turns out to be a getter or setter.
On a plain object the two are often the same. They come apart when the lookup walks the prototype chain. If child inherits a getter from parent, reading child.x finds the getter on parent but runs it with this === child. That split is what makes inherited getters useful.
A proxy can sit anywhere in a prototype chain, so the engine hands the trap both objects. target tells you where to look. receiver tells you on whose behalf you are looking.
| How the operation starts | Value of `receiver` in the trap |
|---|---|
| `proxy.x` or `proxy.x = v` | The proxy itself |
| `child.x` where `child` inherits from the proxy | `child` |
| `Reflect.get(proxy, 'x', other)` | `other`, whatever the caller passed |
| `super.x` inside a method whose home object inherits from the proxy | The current `this` of that method |
The first row covers most code, but the others come up often enough that you should never assume receiver === proxy.
Failure One: Inherited Getters See the Wrong `this`
Here the get trap forwards with and without 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')Reading admin.name finds no own name on admin, so the engine asks admin's prototype, which is the proxy. The proxy's get trap receives target = user and receiver = admin. Reflect.get(target, prop, receiver) runs the getter with this = admin, so it reads admin._name and returns 'Grace'.
If the trap had written return target[prop], the getter would run with this = user and return 'Ada'. Nothing throws. The object just reports the wrong data, and a bug like that can go unnoticed for a long time.
Failure Two: Getters Bypass Your Own Proxy
The receiver matters even with no inheritance involved. When code reads proxy.fullName directly, the receiver is the proxy. If you forward it, any property the getter reads through this also goes through the proxy, so your trap sees those reads too.
const person = {
first: 'Ada',
last: 'Lovelace',
get fullName() { return `${this.first} ${this.last}`; }
};
function makeLogger(forward) {
return new Proxy(person, {
get(target, prop, receiver) {
console.log(`get ${String(prop)}`);
return forward(target, prop, receiver);
}
});
}
const leaky = makeLogger((target, prop) => target[prop]);
console.log(leaky.fullName);
// get fullName
// Ada Lovelace
const faithful = makeLogger((target, prop, receiver) =>
Reflect.get(target, prop, receiver)
);
console.log(faithful.fullName);
// get fullName
// get first
// get last
// Ada LovelaceThe leaky version ran the getter with this = person, the raw target, so this.first and this.last skipped the proxy. For a logger this means missing entries. For a validation layer, an access-control layer, or a reactivity system that tracks which properties a computation depends on, it means the protection or tracking silently fails for anything computed in a getter. Vue 3's reactivity system forwards with Reflect.get(target, key, receiver) for this reason.
Failure Three: Writes Leak Into a Shared Target
The set trap has the same parameter, and getting it wrong here causes worse bugs because data ends up on the wrong object.
const base = new Proxy({}, {
set(target, prop, value, receiver) {
target[prop] = value;
return true;
}
});
const a = Object.create(base);
const b = Object.create(base);
a.color = 'red';
console.log(Object.hasOwn(a, 'color')); // false
console.log(b.color); // 'red'Assigning a.color finds no own color on a, so the engine sends the write up the chain to the proxy with receiver = a. The trap ignores a and writes straight onto the shared target, so every object inheriting from base now sees color: 'red'.
Ordinary JavaScript does something different: an assignment to an inherited writable data property creates a new own property on the object you assigned to. Reflect.set with the receiver reproduces that:
const base = new Proxy({}, {
set(target, prop, value, receiver) {
return Reflect.set(target, prop, value, receiver);
}
});
const a = Object.create(base);
const b = Object.create(base);
a.color = 'red';
console.log(Object.hasOwn(a, 'color')); // true
console.log(b.color); // undefinedReflect.set searches target and its prototypes for a setter or a read-only property. When it finds neither, it defines the property on the receiver. If it finds a setter, it calls the setter with this = receiver. Either way the write lands where the caller expected.
The Forwarding Options Compared
Each common way to forward a get, and what it does:
| Forwarding expression | Inherited getter `this` | Getter's inner reads hit the proxy | Risk |
|---|---|---|---|
| `target[prop]` | target | No | Wrong data, bypassed traps |
| `Reflect.get(target, prop)` | target | No | Same as above; receiver defaults to target |
| `Reflect.get(target, prop, receiver)` | receiver | Yes | Correct default behavior |
| `receiver[prop]` | n/a | n/a | Infinite recursion when receiver is the proxy |
The last row catches people who notice that receiver matters and try to use it directly. When receiver is the proxy, receiver[prop] calls the same get trap again, which calls receiver[prop] again, until the engine throws RangeError: Maximum call stack size exceeded. Use receiver as the third argument to Reflect.get, never as the object you read from.
Gotcha: Forwarded Writes Trigger Other Traps
When Reflect.set forwards with the proxy as receiver and the property is a data property, it finishes the write by calling the receiver's own [[GetOwnProperty]] and [[DefineOwnProperty]] internal methods. The receiver is the proxy, so those calls go through the proxy's getOwnPropertyDescriptor and defineProperty traps if you defined them.
const watched = new Proxy({}, {
set(target, prop, value, receiver) {
console.log(`set ${String(prop)}`);
return Reflect.set(target, prop, value, receiver);
},
defineProperty(target, prop, descriptor) {
console.log(`defineProperty ${String(prop)}`);
return Reflect.defineProperty(target, prop, descriptor);
}
});
watched.x = 1;
// set x
// defineProperty xThis is correct behavior, but it surprises people. A handler that counts writes in both traps will count each assignment twice. A handler whose defineProperty trap rejects some keys will also block assignments to those keys, even though the set trap allowed them. When a handler implements both traps, decide which one owns each policy, or have the set trap skip the work that defineProperty will repeat.
Detecting Inherited Access
Because the receiver identifies the object the operation started on, a trap can tell direct access from inherited access. You need a reference to the proxy, which you can capture after construction:
const handler = {
get(target, prop, receiver) {
if (receiver !== proxy) {
console.log(`inherited read of ${String(prop)}`);
}
return Reflect.get(target, prop, receiver);
}
};
const proxy = new Proxy({ kind: 'base' }, handler);
const child = Object.create(proxy);
proxy.kind; // no log
child.kind; // logs "inherited read of kind"The trap runs only when a property is read, and const proxy is initialized before any read happens, so the trap never sees the variable uninitialized.
When Not to Pass the Receiver
Passing the receiver is the right default, with one important exception. Some built-in getters and methods check that this is a real instance with the right internal slots. Map.prototype.size is a getter that requires this to be an actual Map. If you forward Reflect.get(target, 'size', receiver) with the proxy as receiver, the getter runs with this = proxy and throws a TypeError, because the proxy has no [[MapData]] slot.
For those targets you deliberately pass target as the receiver and bind methods to the target. That choice gives up the benefits described above: the built-in's internals no longer see your proxy. A later chapter on limitations covers this in detail. A practical decision rule:
- The target is an ordinary object or class instance with public properties: forward with
receiver. - The target is a built-in with internal slots (
Map,Set,Date,Promise, typed arrays) or a class with private#fields: forward withtargetfor the affected members, and accept that their internals bypass the proxy. - Unsure: forward with
receiver, and test getters, setters, and subclassing through the proxy.
Exercise
A proxy wraps const counter = { _n: 0, get n() { return this._n; }, set n(v) { this._n = v; } } with a set trap that does target[prop] = value; return true;. Code runs const c = Object.create(proxy); c.n = 5;. Which object ends up with _n === 5, and what does c.n return? What one change fixes it?
Show answer
The setter runs with this = counter (the target), so counter.n becomes 5 and c gets no own n. Reading c.n goes through the proxy to the getter. A get trap that forwards with the receiver, or no get trap at all, runs the getter with this = c. Because c has no own n, the read falls back to the inherited counter.n, so c.n returns 5. But the state is shared: a second object created from the proxy would also see 5. The fix is to forward with return Reflect.set(target, prop, value, receiver);, which runs the setter with this = c, so c gets its own _n.
Exercise
Why does get(target, prop, receiver) { return receiver[prop]; } crash on proxy.anything, while Reflect.get(target, prop, receiver) does not?
Show answer
For proxy.anything the receiver is the proxy itself, so receiver[prop] performs another [[Get]] on the proxy, which calls the same trap again, recursing until a RangeError. Reflect.get(target, prop, receiver) reads from target, which has no trap, and only uses receiver as the this value for getters.
Exercise
A handler has a set trap and a defineProperty trap, each of which increments a shared writes counter before forwarding with Reflect. After proxy.a = 1; proxy.b = 2;, what is writes, and why?
Show answer
4. Each assignment hits the set trap once. Reflect.set then defines the data property on the receiver, which is the proxy, so the defineProperty trap also runs once per assignment.