Create a Proxy that passes reads straight through.
Acceptance criteria
- 1.#out should show Ada.
JavaScript Tutorial · JavaScript Performance
These 10 tasks go with the JavaScript Proxy chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A Proxy wraps an object and lets you intercept what happens when it is used.” .
1.Intercept a readexercise · easy · 10 min
Create a Proxy that passes reads straight through.
Passes when: #out should show Ada.
2.Log every readexercise · easy · 10 min · practice plan
Record which keys were read through the proxy.
Passes when: #out should show name,age.
3.Validate a writeexercise · easy · 10 min · practice plan
Reject a negative age through a set trap.
Passes when: #out should show 0.
4.Supply a default for a missing keyexercise · easy · 10 min · practice plan
Return a fallback string for any property not on the object.
Passes when: #out should show not set.
5.Use the has trapexercise · medium · 10 min · practice plan
Make one extra key always report as present.
Passes when: #out should show true and false.
6.Fix the set trap that forgets to store the valuebug fix · easy · 8 min
The trap must actually assign the value on the target.
Passes when: #out should show 40.
7.Fix the set trap missing its return valuebug fix · easy · 8 min · practice plan
A set trap must return true, or the assignment is reported as failed even though the value was stored.
Passes when: #out should show assignment accepted.
8.Fix the validation checking the wrong keybug fix · easy · 8 min · practice plan
The validation compares against the wrong property name.
Passes when: #out should show 0.
9.Fix the direct target read counted as a proxy readbug fix · easy · 8 min · practice plan
Reading the original target bypasses the trap entirely, so nothing is counted.
Passes when: #out should show 2 trapped.
10.Fix the has trap that ignores the real objectbug fix · easy · 8 min · practice plan
Any key not specially handled should fall back to checking the object.
Passes when: #out should show true.
Between them these tasks cover Proxy, get and set traps and Validation.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.