Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 14.6 — Proxy and Reflect
JavaScript · Example 3 of 3 ES2015+

The idea behind reactive frameworks

No code calls render() after changing the count: the proxy notices writes. Setting the same value twice doesn't re-render. Click +1 to see more lines.

A Proxy wraps an object and lets you intercept basic operations on it (reading, writing, deleting, in, calling) with handler functions called traps; Reflect offers the default version of each operation, so a trap can add behaviour and then do the normal thing.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write readOnly(obj) that returns a proxy where reads work but any write or delete throws Error("read-only"). Show both behaviours.

02

Write a proxy env where reading a missing key throws ReferenceError("Unknown setting: <key>") instead of returning undefined, so typos are caught.

Explain it without notes

01

What is a Proxy and what is a trap? Give three traps and the operations they intercept.

02

Why is Reflect usually used inside traps?

03

How would you validate property values with a Proxy, and how does strict mode affect it?

04

Explain how a Proxy can make UI reactive, and what its limits are.

The idea behind reactive frameworks ES2015+

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

count: 0 -> 5
count: 5 -> 6