Bayu stands before the cabinet as a Proxy: every read, write, delete or JSON.stringify passes his rule book (the handler) and every trap the engine calls is…
Why it matters
Proxy is the engine behind Vue 3 and MobX reactivity, defaults, write validation and test mocks. Understanding it means understanding how a framework "knows" your data changed, and why a write that does not go through the proxy slips past it.
The key idea
A proxy keeps nothing: it intercepts, then passes on
Common mistakes
Confused: does a proxy change the original object? Making a proxy neither changes nor copies target. But a proxy keeps nothing of its own: a write through p goes on to target (unless your trap holds it back), so target changes. A write straight to target passes every trap by.
Using a Proxy on a very hot path Every access through a proxy runs a trap function: one JSON.stringify(p) on three keys is already 8 calls. Use it for APIs, config and reactivity, not in a number-crunching loop that reads millions of times.
Forgetting return true in the set trapset must return a truthy value. If not, strict code (modules, classes) throws a TypeError, even though t[k] = v may already have run. Just return Reflect.set(t, k, v, r).
A get trap answering differently for a frozen property The engine checks invariants: for a target property that can be neither written nor reconfigured (after Object.freeze, say), get must give its real value. If not: a TypeError.
Try it in this playground
Write through p, then look at the target With no set trap, the write is passed on: the cabinet changes.
Give a default for a missing drawer The get trap answers by itself, storing nothing.
Refuse a negative balance A set trap that refuses: the caller gets an error, the cabinet is safe.
Write straight to target No trap is called: Bayu knows nothing about it.