Proxy and Reflect — intercepting property access¶
Level: 301 · for anyone who wants to log, check or fake property access
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: A Proxy sees only the operations you trap — "a" in p never reaches a get trap, because in is the has trap — and it cannot lie about a frozen target: returning 0 for a frozen answer: 42 throws a TypeError.
Keywords: Proxy, Reflect, Proxy.revocable
What the finished page will answer¶
- Which operations does a
gettrap see: does"a" in p,Object.keys(p)orp.missinggo through it? - Why does a trap that returns
0for a frozenanswer: 42throw aTypeError, and which other invariants does the engine check? - What does
Reflect.get(target, key, receiver)do thattarget[key]does not, when the target has a getter? - Why does
Reflecthave exactly 13 functions? - What does a revoked proxy from
Proxy.revocablethrow, and can a program tell a proxy from its target?
Examples it will need¶
-
proxy_and_reflect_trap_log_js.js— a log of which traps fire for a read, in, Object.keys, an assignment and delete on one proxy -
proxy_and_reflect_invariants_js.js— the TypeError from a get trap that lies about a frozen property, and the error from a revoked proxy
See also¶
- Property descriptors — the non-writable, non-configurable properties a proxy cannot lie about
freeze,sealandpreventExtensions— the frozen targets whose invariants every trap must keep- Getters and setters — the receiver that Reflect.get passes on
- Well-known symbols — the other way to hook what the language does to an object