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

Building the Proof Card Export

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 MappingProxyType becomes Object.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. const only keeps the name from being reassigned.
  • Python’s class attribute becomes a defaults object, and Object.create(defaults) makes the card with defaults as its prototype. The title and price are assigned to the card itself. card.category finds "print", but Object.hasOwn(card, "category") is false: like a class attribute, it’s shared, but there’s no class, only a prototype link to a plain object.
  • Python’s read-only id property becomes an own data property from Object.defineProperty(), with all three flags written out. card.id gives "P-17", and assigning to it throws a TypeError in a module, where Python raises an AttributeError. It isn’t enumerable, so Object.keys(card) lists only title and price, but that hides it from listings, not from code. Python’s underscore hides nothing (vars(card) lists _id); neither is privacy.
  • Python’s total property, the run’s cost (price times copies, not the order total from earlier), becomes an accessor. The descriptor’s get key holds a function, written here as a method, not the get total() syntax; it runs on every read, with this as the card. card.total gives 300. It isn’t enumerable either, so listings skip it, like the ID.
  • Python’s id and total properties sit on the class, like category; the port makes both the card’s own, so Object.hasOwn() says true for each.
  • The SOURCE symbol tags the card with "proof desk" without a string key that could clash with real data. Python’s nearest idea is a saved object() 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 a TypeError, so the script builds a dict. JavaScript’s JSON.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:

Language: JavaScript
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));

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 You’ll port Harbour Prints’ proof card script from Python. Its settings are a read-only mapping; each card has a default category, its own title and price, and read-only ID and total properties.

00:14 The customer sees only the ID, title and copies; the price, the total and the port’s source tag stay in-house. So the script builds its report by hand.

00:26 Where Python wraps a dict in a mapping proxy, JavaScript freezes a plain object. One shallow freeze covers this flat record, unlike the earlier nested config.

00:39 But a mapping proxy is only a read-only view: whoever holds the dict can still change it; the freeze locks the object itself. And const only keeps the name from being reassigned.

00:53 Python’s class attribute becomes a defaults object, and Object.create() makes the card with defaults as its prototype. The title and price are assigned to the card itself.

01:05 card.category finds “print”, but Object.hasOwn() says the card doesn’t own it. Like a class attribute, it’s shared, but there’s no class: only a prototype link to a plain object.

01:20 for...in would list category too, as you’ve seen. Python’s ID is a read-only property over an underscore attribute. Object.defineProperty() gives the card its own id through a descriptor.

01:36 The port writes all three flags, though left out they’d default to false. card.id gives P-17. Assigning to it throws a TypeError in a module, where Python raises an AttributeError.

01:51 Object.keys() skips the non-enumerable ID: only the title and price. That hides it from listings, not from code; Python’s underscore hides nothing, and neither is privacy.

02:06 Python’s total gives the run’s cost, price times copies, not the order total from earlier. A second Object.defineProperty() call makes it an accessor, whose get key holds a method, not the get total() syntax.

02:20 It runs on every read, with this as the card. card.total calls it: 12 times 25 gives 300. It isn’t enumerable either, so listings skip it, like the ID.

02:35 Python’s properties sit on the class, like category; the port makes both the card’s own.

02:43 The proof desk tags the card with a key that can’t clash with real data: one symbol, saved in a constant. The bracket assignment stores “proof desk” under it.

02:55 Python’s nearest idea, a saved object() sentinel, can only key a dict, not name an attribute on the card. And a symbol isn’t a secret: anyone can list symbol keys.

03:09 Python’s json.dumps() refuses the card itself, so the script builds a dict. JavaScript’s JSON.stringify() accepts it, so why not dump the card?

03:21 As you’ve seen, it keeps only own, enumerable string keys: the title and price. The ID and total aren’t enumerable, the category’s inherited, the tag’s a symbol.

03:33 And the price leaks, which the customer mustn’t see.

03:39 So the port keeps Python’s report: an object literal reading each field by name, with a dot where Python indexes. console.log() and template literals stand in for print and F strings; with no implicit string joining, a plus joins the halves.

03:57 JSON.stringify() is compact by default, which is why Python passes separators. Run it with node, beside a package.json that makes it a module.

04:08 Both lines match Python’s exactly. Only the three public fields leave the card.

04:16 Build the card on purpose: own data, inherited defaults, chosen flags, and a symbol for bookkeeping. Freeze the settings you mean to fix. Export by name, never by dumping the object.

04:32 Next up is the exercise Finish the Proof Export. You’ll report proof cards P-24 and P-31: their own ID, title and copies, and nothing inherited, hidden or symbol-keyed.

Become a Member to join the conversation.