snippetTip
Proxy and Reflect for metaprogramming in JavaScript
Viewed 0 times
ProxyReflecthandlertrapgetsetreactive
browsernodejs
Problem
Need to intercept property access, validation, logging, or create reactive objects without manual getter/setter boilerplate.
Solution
Use Proxy for transparent interception. Common uses: (1) Validation proxies — type check on set. (2) Observable/reactive objects (Vue 3 uses this). (3) Auto-vivification — create nested objects on access. (4) Logging property access. (5) Default values for missing properties. (6) Revocable access with Proxy.revocable(). Use Reflect to forward to default behavior within traps.
Why
Proxy intercepts fundamental operations at the language level. Unlike Object.defineProperty (per-property), Proxy handles all properties including ones that don't exist yet.
Revisions (0)
No revisions yet.