JavaScript, inside out · Chapter 43 of 51
Proxy and Reflect
By the end of this chapter you can decide what happens when code reads, writes, checks or deletes a property on an object. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Under the hood
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Three bugs reached Kabir in one week. The rider app showed undefined where a status label should be, because nobody had translated "Out for delivery" into Kannada yet. A script set an order's quantity to -3, and Tapri refunded a customer for three cups of chai they never bought. And a partner café that only needed to read the menu changed the price of bun maska to ₹0.
With what you know so far, you can check values in every function that touches an order, and hope nobody forgets. Object.freeze stops changes, but only one level deep, and it freezes Tapri's own menu too. A getter or setter works for a key you know in advance, not for every key, nor for in, delete or Object.keys.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
withDefault: a fallback for missing labels
Write withDefault(target, fallback). Return a proxy of target (a live view, not a copy: a key added to target later is seen through it). Reading a key that target has gives its value as it is, even a falsy one such as 0, '' or false. Reading a key it does not have gives fallback instead of undefined, whatever fallback is (it may be 0).
Only reading changes: the in operator still says false for a missing key.
- Task 2
validatedOrder: refuse bad values as they are set
Write validatedOrder(order). Return a proxy of order that checks values as they are set. item must be one of MENU's own keys ('masala chai', 'cutting chai', 'bun maska' or 'parle-g'), otherwise throw a TypeError. qty must be a whole number from 1 to 20, otherwise throw a RangeError: -3, 0, 21, 2.5, the string '2' and null are all refused.
A refused value is not stored: the order keeps its old value. Good values, and any other key (such as note), are stored on the order as normal.
- Task 3
hidePrivate: hide the underscore fields
Write hidePrivate(target). Return a proxy of target where every key that starts with an underscore looks as if it did not exist: reading it gives undefined, the in operator says false, and Object.keys and JSON.stringify leave it out. Other keys behave as normal, and a key that is really missing still gives false for in.
For { name: 'Kiran', _phone: '98450 12345' }, Object.keys gives ['name'] and JSON.stringify gives {"name":"Kiran"}. The original object keeps its underscore fields.
- Task 4
readOnly: a read-only view of the menu, all the way down
Write readOnly(target). Return a read-only view of target. Reading works as normal, but setting a property, deleting one or defining one with Object.defineProperty throws a TypeError. Objects and arrays you read from the view are read-only views too, at every depth, so view.items[1].price = 0 and view.items.push(something) also throw a TypeError.
The original object is never changed through the view, and it stays a live view: when Tapri changes its own menu, the view shows the new value.
- Task 5 · interview question
Interview: observe, a store that reports every change
Write observe(target, onChange). Return a proxy of target that calls onChange(key, newValue, oldValue) after every real change has been stored. Setting a key to a different value is a change (a new key has the old value undefined); Object.is decides whether two values are different, so setting a key to the value it already has does not call onChange.
Deleting a key that existed removes it from target and calls onChange(key, undefined, oldValue). Deleting a key that was not there does not call onChange.
What you will learn
- new Proxy(target, handler): an object that sends every operation through your handler first.
- Traps: get, set, has, deleteProperty, ownKeys and defineProperty, one per kind of operation.
- Reflect: one method per trap that does the normal thing.
More in Under the hood
Build "Proxy and Reflect" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.