Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Accepting or Rejecting a Setting

Accepting or Rejecting a Setting

In this lesson, you port the settings library’s write validator to JavaScript. The settings are the same flat pair, a level of 2 and a label, "desk", and a level must be a whole number from 1 to 5.

In Python, the Settings class checks writes in __setattr__(), which runs on every attribute assignment. A bad level is logged as rejected, and a bare return skips the store, so a stray line can’t stop the program:

Language: Python
class Settings:
    def __init__(self, level, label):
        self.level = level
        self.label = label

    def __setattr__(self, name, value):
        if name == "level" and value not in range(1, 6):
            print("rejected:", value)
            return
        super().__setattr__(name, value)

settings = Settings(2, "desk")
settings.level = 9
print(settings.level)

Setting level to 9 prints rejected: 9, and level is still 2:

Language: Program Output
rejected: 9
2

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll port the settings library’s write validator to JavaScript. The settings are the same flat pair, a level of 2 and a label, desk, and a level must be a whole number from 1 to 5.

00:14 In Python, the Settings class checks writes in __setattr__(), which runs on every attribute assignment. A bad level is logged as rejected, and a bare return skips the store, so a stray line can’t stop the program.

00:29 Setting level to 9 prints rejected 9, and level is still 2.

00:35 In JavaScript, the hook goes on a proxy, as the read audit’s get trap did. Its handler gets a set trap, which runs for every assignment through the proxy, though a write on settings itself skips it.

00:48 It receives the target, key, value, and receiver, the object written through. The check carries over, with && for and, and an exclamation mark for not.

01:00 Like Reflect.get() for reads, Reflect.set() is the default write, JavaScript’s super().__setattr__(), returning true when the write succeeds.

01:12 The set trap answers with a boolean, so a refused write returns false where Python had a bare return. Then it sets level to 9 through view.

01:24 Python printed rejected 9 and kept level at 2. What does JavaScript do with view.level = 9? Your turn: pause the video and predict.

01:40 JavaScript logs rejected 9, then stops with a TypeError: the set trap returned false, so the assignment was refused.

01:49 The last line never runs.

01:53 Python ignores what __setattr__() returns: skipping the store is the rejection. A set trap’s boolean is its answer: false means the assignment was refused.

02:05 The message says falsish because anything that counts as false refuses too, even the undefined a bare return; gives back, like Python’s None.

02:15 And a .mjs file is an ECMAScript module, which always runs in strict mode, where some silent failures throw errors. In strict mode, a refused assignment throws a TypeError.

02:27 Don’t return true to hush it: the caller would think the write worked.

02:33 So keep the false, and let the caller decide. A caller that wants a yes or no calls Reflect.set() on view itself: it runs the trap and returns false, with no error.

02:46 A plain assignment still throws, so wrap it in try and catch, like Python’s try and except, and log err.name, the error’s name. The output shows both writes refused, and level is still 2.

03:01 The callers hear about a refusal, so the trap no longer prints it. The trap keeps Reflect.set()’s boolean in ok, logs the write only when ok is true, and returns that same boolean.

03:15 Why check? The target can refuse too, a read-only property for example: where Python’s default write would raise an attribute error, Reflect.set() returns false, and the log mustn’t record a write that didn’t happen.

03:30 Setting level to 4 logs write level 4. The check refuses the 9 before Reflect.set(), so nothing is logged, and level stays 4.

03:42 Like super().__setattr__(), which still runs a property’s setter, Reflect.set() does the whole default assignment. A setter on level would run with this on the proxy, thanks to the receiver.

03:56 And if you added a defineProperty trap, the hook for defining a property, the default write would pass through it too. So keep the audit in the set trap alone: a second logging trap would log every write twice.

04:11 So check the value in a set trap and return false to refuse it. On success, return Reflect.set()’s own boolean, and log only when it’s true. A plain assignment in a module then throws a TypeError, while Reflect.set() on the proxy returns false.

04:29 Next up is the exercise Validate the Retry Budget. There, the retry count’s limit is another setting, and that limit needs protecting too, so this level check won’t copy over unchanged.

Become a Member to join the conversation.