Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Proxy `get` and `set` traps and the `receiver` parameter

The `get` and `set` traps intercept property reads and writes. Passing `receiver` to `Reflect` preserves the receiver of the original operation.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

const proxy = new Proxy(target, {
  get(target, key, receiver) {
    return Reflect.get(target, key, receiver);
  },
  set(target, key, value, receiver) {
    return Reflect.set(target, key, value, receiver);
  },
});

In new Proxy(target, handler), get(target, key, receiver) intercepts the internal [[Get]] operation and returns the value read. set(target, key, value, receiver) intercepts [[Set]] and reports whether the write succeeded. key is a string or a Symbol. If the relevant trap is absent, the proxy forwards the operation to target with the same receiver.

target is the wrapped object. receiver is the this value supplied to a getter or setter. For ordinary direct access it's the proxy; when access reaches the proxy through the prototype chain, it can be the object that inherits from the proxy. For an ordinary data-property write, receiver is where the property may be created or updated.

To preserve those semantics, return Reflect.get(target, key, receiver) and Reflect.set(target, key, value, receiver) from the traps. Omitting receiver from those calls defaults it to target. Reading or assigning target[key] directly also uses target as the receiver. Repeating the operation on the proxy itself inside the trap can re-enter the trap and recurse.

The set trap's return value is converted to a boolean. true reports success but does not itself write anything. If [[Set]] returns false, strict-mode assignment throws TypeError, non-strict assignment does not throw for that reason, and Reflect.set returns false.

The engine enforces invariants for non-configurable own properties of target. For a non-writable data property, get must return the same value as tested by Object.is, and set cannot report success for a different value. For an accessor property with no getter, get must return undefined; with no setter, set cannot report success. Violating these rules throws TypeError regardless of strict mode.

Examples

{
  const target = {
    points: 1,
    get score() { return this.points; },
    set score(value) { this.points = value; },
  };

  const proxy = new Proxy(target, {
    get(target, key, receiver) {
      return Reflect.get(target, key, receiver);
    },
    set(target, key, value, receiver) {
      return Reflect.set(target, key, value, receiver);
    },
  });

  const child = Object.create(proxy);
  console.log(`before: ${child.score}`);
  child.score = 7;
  console.log(`child: ${child.score}`);
  console.log(`target: ${target.score}`);
}

Result

before: 1
child: 7
target: 1

Inside the `score` getter and setter, `this` is `child`. The first read finds inherited `points`; writing through the setter creates `child.points` as an own property and leaves `target.points` unchanged.

Related lessons

  • JavaScript · Lesson 29 · Advanced · Version: 4

Sources