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

When a Proxy Must Tell the Truth

When a Proxy Must Tell the Truth

In this lesson, you see what a proxy may report about frozen settings. The settings library ships its settings frozen, and a dashboard test uses a stub instead, one that answers every read with 99, to try an out-of-range level.

In Python, the shipped Settings is a frozen dataclass, so assigning to level raises a FrozenInstanceError. The Stub class wraps the settings, and its __getattr__() returns 99 for anything the stub itself doesn’t have:

Language: Python
from dataclasses import dataclass

@dataclass(frozen=True)
class Settings:
    level: int
    label: str

class Stub:
    def __init__(self, target):
        self._target = target

    def __getattr__(self, name):
        return 99

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

Freezing blocks writes, not what a wrapper reports, so the log shows 2 from the settings, then 99 from the stub:

Language: Program Output
2
99

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 see what a proxy may report about frozen settings. The settings library ships its settings frozen, and a dashboard test uses a stub instead, one that answers every read with 99, to try an out-of-range level.

00:16 In Python, the shipped Settings is a frozen dataclass, so assigning to level raises a FrozenInstanceError. The Stub class wraps the settings, and its __getattr__() returns 99 for anything the stub itself doesn’t have.

00:31 Freezing blocks writes, not what a wrapper reports, so the log shows 2 from the settings, then 99 from the stub.

00:40 In JavaScript, Object.freeze() makes each of an object’s own properties read-only and fixed in place, and stops it gaining or losing properties.

00:51 The port wraps the frozen settings in a proxy whose get trap returns 99: the same stub. As you saw earlier, a get trap runs for every read through the proxy.

01:03 The script logs settings.level, then stub.level. The title hints it won’t be Python’s 99. So does reading stub.level give the real 2, or something else?

01:16 Your turn: pause the video and predict.

01:24 The first line is 2, as in Python, but reading stub.level throws a TypeError.

01:33 The message spells it out. level is a read-only and non-configurable data property on the target, and the proxy didn’t return its actual value: expected 2, but got 99.

01:46 Creating the proxy worked, and the trap did run and return 99. Then JavaScript checked that answer against the target’s property and threw instead of passing it on.

01:56 A rule that every proxy answer must keep, like this one, is called an invariant. Python has no such check: a frozen dataclass blocks writes in its __setattr__(), and the stub keeps the settings in _target, but nothing ties its answers to them.

02:16 Which properties does the rule protect? As you saw with archived, Object.getOwnPropertyDescriptor() returns a property’s value and its flags.

02:26 For the frozen level, writable and configurable are both false. For an unfrozen draft of the same settings, both are true.

02:36 Put the same stub in front of the draft, and it logs 99. A property that can’t be changed or redefined has one true value, so every proxy has to report it.

02:48 With either flag true, the proxy may answer differently. In Python, even a read-only property doesn’t stop a wrapper from reporting 99, because the wrapper is a separate object.

03:00 A JavaScript proxy stands in for its target, so it’s held to each property’s flags.

03:08 So a proxy in front of frozen settings forwards their values exactly. The audit’s logging and forwarding from earlier still fit: it logs the key, then returns Reflect.get(), the target’s own answer.

03:21 Reading level and label through it shows each read, then 2 and desk.

03:27 A trap can still log a read or throw its own error; it can’t change a fixed value. When a test really needs 99, give the stub a target that allows it, such as an empty object, never the shipped settings.

03:43 So a proxy adapts how its target behaves, like the set trap that refused bad writes, but the target’s constraints still apply. When a property is non-writable and non-configurable, forward its value exactly.

03:58 Other traps have invariants too: a frozen object can’t gain or lose keys, so an ownKeys trap, which lists them, must list every one. You won’t write that trap here, or the one that describes a property: JavaScript checks both against the target, the same way it checks get.

04:18 Next up is the exercise Preserve a Fixed Identifier. There, you’ll take settings whose id, ST-24, is fixed, and give them a missing-setting fallback that stays truthful.

Become a Member to join the conversation.