Skip to content
dreamcode
dreamcode
Map
Metaprogramming
Lesson 46 of 48
+15 XP on finish
JS EXPERTChapter 9 · JS Expert

Object observation with Proxies

A Proxy wraps an object to intercept core operations like reads, writes, and key lookups. Combined with Reflect, it powers modern reactive frameworks.

Worked example

How it reads

  • new Proxy(target, handler) creates an interceptor shell
  • get(obj, prop) intercepts property lookups on the target object
Cloud tip: Always return Reflect.get(...) inside proxies when forwarding original behavior to target objects.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Make the set trap refuse negative values: log rejected -5 and return true without storing it. Try obs.stars = -5, then log stats.stars (20).

Press Run to check your work.