Building the Proof Card Export
You built Harbour Prints’ proof card export in JavaScript: the card with its inherited default, its own ID, a live total and a source tag, the frozen settings, and one public report for proof P-17. The customer sees only the ID, title and copies, so the report names those fields on purpose instead of dumping the card.
- Python’s
MappingProxyTypebecomesObject.freeze()on a plain object. The settings record is flat, so one shallow freeze covers it; the earlier config needed its nested limits frozen separately. A mapping proxy is only a read-only view, so whoever holds the dict can still change it, while the freeze locks the object itself.constonly keeps the name from being reassigned. - Python’s class attribute becomes a
defaultsobject, andObject.create(defaults)makes the card withdefaultsas its prototype. The title and price are assigned to the card itself.card.categoryfinds"print", butObject.hasOwn(card, "category")isfalse: like a class attribute, it’s shared, but there’s no class, only a prototype link to a plain object. - Python’s read-only
idproperty becomes an own data property fromObject.defineProperty(), with all three flags written out.card.idgives"P-17", and assigning to it throws aTypeErrorin a module, where Python raises anAttributeError. It isn’t enumerable, soObject.keys(card)lists onlytitleandprice, but that hides it from listings, not from code. Python’s underscore hides nothing (vars(card)lists_id); neither is privacy. - Python’s
totalproperty, the run’s cost (price times copies, not the order total from earlier), becomes an accessor. The descriptor’sgetkey holds a function, written here as a method, not theget total()syntax; it runs on every read, withthisas the card.card.totalgives300. It isn’t enumerable either, so listings skip it, like the ID. - Python’s
idandtotalproperties sit on the class, likecategory; the port makes both the card’s own, soObject.hasOwn()saystruefor each. - The
SOURCEsymbol tags the card with"proof desk"without a string key that could clash with real data. Python’s nearest idea is a savedobject()sentinel, but a sentinel can only key a dict: it can’t name an attribute on the card. A symbol key isn’t a secret: anyone can list an object’s symbol keys. - Python’s
json.dumps(card)raises aTypeError, so the script builds a dict. JavaScript’sJSON.stringify()accepts the card, but it keeps only own, enumerable string keys, so it gives{"title":"Harbour","price":12}: no ID or total (not enumerable), no category (inherited), no tag (a symbol), and the price leaks, which the customer mustn’t see.
Here’s the port, proof.js:
const settings = Object.freeze({ copies: 25 });
const SOURCE = Symbol("source");
const defaults = { category: "print" };
const card = Object.create(defaults);
card.title = "Harbour";
card.price = 12;
Object.defineProperty(card, "id", {
value: "P-17",
writable: false,
enumerable: false,
configurable: false,
});
Object.defineProperty(card, "total", {
get() {
return this.price * settings.copies;
},
enumerable: false,
configurable: false,
});
card[SOURCE] = "proof desk";
const report = {
id: card.id,
title: card.title,
copies: settings.copies,
};
console.log(
`Proof ${report.id}: ${report.title}, ` +
`${report.copies} copies`,
);
console.log(JSON.stringify(report));