Congratulations, you made it to the end of the course! You ported the settings library’s Python hooks to JavaScript. Every property read of the desk’s settings now lands in the audit, an assigned level of 9 never gets in, and each audit event keeps its values apart from the text.
Here’s the project’s code once more, plus a Symbol.toStringTag label on the record, so the audit also captures a protocol lookup. The script then sets level to 4, uses Reflect.set() to try setting it to 9, asks for the diagnostic label, and logs the events:
const events = [];
function audit(strings, ...values) {
return { parts: [...strings], values };
}
function isValidLevel(value) {
return Number.isInteger(value) && value >= 1 && value <= 5;
}
function createSettings() {
const record = {
level: 2,
label: "desk",
[Symbol.toStringTag]: "Settings",
};
return new Proxy(record, {
get(target, key, receiver) {
events.push(audit`read ${key}`);
return Reflect.get(target, key, receiver);
},
set(target, key, value, receiver) {
if (key === "level" && !isValidLevel(value)) {
return false;
}
const ok = Reflect.set(target, key, value, receiver);
if (ok) {
events.push(audit`write ${key} ${value}`);
}
return ok;
},
});
}
const settings = createSettings();
settings.level = 4;
console.log(Reflect.set(settings, "level", 9));
console.log(Object.prototype.toString.call(settings));
console.log(events);
The attempt to set level to 9 returns false and leaves no event, and the label lookup reaches the trap under its symbol key:
false
[object Settings]
[
{ parts: [ 'write ', ' ', '' ], values: [ 'level', 4 ] },
{ parts: [ 'read ', '' ], values: [ Symbol(Symbol.toStringTag) ] }
]
One shared log is fine here because the script builds one desk. “Isolate Two Settings Audits” showed what changes with two.
In the project’s Python version, assigning 9 would raise a ValueError. The nearest thing to the label lookup is repr(settings), which finds __repr__() on the class directly and skips even __getattribute__(). No hook hears it, so the log holds only the write.
In this course, you learned how to: