Intercepting Every Property Read
In this lesson, you port the settings library’s missing-setting fallback to JavaScript, the start
of a read audit. The settings are flat: a level of 2 and a label, "desk".
In Python, the library’s Settings class has a __getattr__() fallback that logs a missing
setting and returns "unset". Python calls __getattr__() only when the normal lookup doesn’t find
the attribute:
class Settings:
def __init__(self, level, label):
self.level = level
self.label = label
def __getattr__(self, name):
print("missing:", name)
return "unset"
settings = Settings(2, "desk")
print(settings.level)
print(settings.theme)
So the log shows 2 for level with no message, then missing: theme and unset:
2
missing: theme
unset
In JavaScript, the hook for property reads lives on a separate object: a proxy. new Proxy() takes
the target, here settings, and a handler object, and returns a new object that stands in front of
the target. The handler’s methods are called traps.
The get trap is the handler’s hook for property reads; it receives the target and the key. Here,
get is the method’s name, written like a class method, not a getter. So a line-by-line port puts __getattr__()’s body in the trap: log the missing key, return "unset". Then the script reads
level and theme through view:
const settings = { level: 2, label: "desk" };
const view = new Proxy(settings, {
get(target, key) {
console.log("missing:", key);
return "unset";
},
});
console.log(view.level);
console.log(view.theme);
Python printed 2 for level, with no message, but JavaScript logs missing: level, then unset:
the 2 never comes back. theme gets the same two lines:
missing: level
unset
missing: theme
unset
Python’s attribute lookup finds level on the instance first, so __getattr__() only hears about
what’s missing. A proxy stores no settings itself, so there’s nothing to find first: every property
read through it goes to the handler. With a get trap, the trap answers every read, found or not.
The Python twin is __getattribute__(), which runs on every attribute read. The analogy stops at
where the hook lives: it sits on the class, so every read of the instance goes through it, while a
proxy is a separate object in front of the target.
Every read reaching the trap is a bug for the fallback, and exactly what the audit needs. So keep
the every-read hook: log each read, and forward it. With no get trap at all, a proxy forwards each
read to the target. Inside a trap, Reflect.get() does that default read for you, like
super().__getattribute__() in Python. Reflect is a built-in object that holds the default
version of each object operation.
The trap gains a third parameter, receiver: the object the read went through, here view. Pass
it along to Reflect.get(); the next lesson shows when it matters.
In Python, a missing attribute makes super().__getattribute__() raise an AttributeError, and
that error calls __getattr__(). Reflect.get() returns undefined, JavaScript’s value for a
missing property. So there’s no fallback: the trap decides.
key in target is true when the target has that key, like in on a Python dict, plus inherited
keys, the way Python’s attribute lookup also checks the class. If it does, the trap forwards the
read; otherwise it returns "unset", the library’s default. The last two lines check the proxy
against the target and read the target directly:
const settings = { level: 2, label: "desk" };
const view = new Proxy(settings, {
get(target, key, receiver) {
console.log("read:", key);
if (key in target) {
return Reflect.get(target, key, receiver);
}
return "unset";
},
});
console.log(view.level);
console.log(view.theme);
console.log(view === settings);
console.log(settings.level);
Now the log shows read: level, then 2, and read: theme, then unset:
read: level
2
read: theme
unset
false
2
For objects, === checks identity, like Python’s is, and view === settings is false. And a
read straight from settings goes past the audit: settings.level logs 2 with no read: line.
So the audit only sees code that holds the proxy: give the rest of the code view, never
settings. The proxy is a convention of your API, not a lock.
So to audit reads in JavaScript, put a proxy in front of the object, log in its get trap, and
forward with Reflect.get(), passing the receiver along, and choose the missing-key default
yourself. The trap sees every read, not only the missing ones, and only the reads that go through
the proxy.
Next up is the text lesson Keeping Accessors on the Right Receiver. There, you’ll give the
settings a label getter that reads this.level and see why Reflect.get() takes the receiver.