Skip to main content

JavaScript Proxy — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Proxy lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Intercept a read

    easy10 min

    Create a Proxy that passes reads straight through.

  2. 2

    Log every read

    easy10 min

    Record which keys were read through the proxy.

  3. 3

    Validate a write

    easy10 min

    Reject a negative age through a set trap.

  4. 4

    Supply a default for a missing key

    easy10 min

    Return a fallback string for any property not on the object.

  5. 5

    Use the has trap

    medium10 min

    Make one extra key always report as present.

Find the bug(5)

  1. 1

    Fix the set trap that forgets to store the value

    easy8 min

    The trap must actually assign the value on the target.

  2. 2

    Fix the set trap missing its return value

    easy8 min

    A set trap must return true, or the assignment is reported as failed even though the value was stored.

  3. 3

    Fix the validation checking the wrong key

    easy8 min

    The validation compares against the wrong property name.

  4. 4

    Fix the direct target read counted as a proxy read

    easy8 min

    Reading the original target bypasses the trap entirely, so nothing is counted.

  5. 5

    Fix the has trap that ignores the real object

    easy8 min

    Any key not specially handled should fall back to checking the object.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace