Checking Who Owns a Property
In this lesson, you check which of a proof card’s properties belong to the card itself and which
come from a shared default. At Harbour Prints, the Python Card class holds the default as a class
attribute, category = "print", and each instance gets its own title and price in __init__.
card.category finds "print" on the class.
The export writes out each field a card supplies itself, so a loop over vars(card), the
instance’s own __dict__, collects only ['title', 'price'].
In JavaScript, the default goes in a plain object literal, and Object.create(defaults) makes a
new, empty card linked to it. Dot assignment adds title and price to the card itself. Reading
card.category finds nothing on the card, so JavaScript follows the link and finds "print".
Python looks from the instance to its class; JavaScript looks from the object to the object it’s
linked to, with no class involved. That link is the object’s prototype.
Ported, the Python loop becomes a for...in loop, which visits an object’s keys the way
for key in walks a dict. There’s no vars() to port, since the card is already a plain object of
keys. The loop that pushes each key into an array collects one key too many:
const defaults = { category: "print" };
const card = Object.create(defaults);
card.title = "Harbour";
card.price = 12;
const looped = [];
for (const key in card) {
looped.push(key);
}
console.log(looped);
The export would write the shared category as the card’s own:
[ 'title', 'price', 'category' ]
for...in visits the card’s own keys, then walks the link and visits the listable keys of the
defaults too. Every key you assign is listable; the next lesson shows how a key can be kept out.
Python’s vars(card) stops at the instance, and for...in behaves more like dir(), which also
looks at the class.
Object.keys() lists only the object’s own listable keys and gives [ 'title', 'price' ]. On an
object literal with no link, the two agree, which is why they get mixed up.
The same split shows up in a check for one key. JavaScript’s in operator works like hasattr():
"category" in card walks the link and gives true. Watch the word: Python’s in on a dict checks
only that dict. Object.hasOwn(card, "category") asks the card alone, like in on vars(card),
and gives false.
| JavaScript | Looks along the link | Result for card |
Python counterpart |
|---|---|---|---|
for...in |
yes | title, price, category |
closest: dir(card), which also looks at the class (but sorts, and lists methods and dunders too) |
Object.keys() |
no | title, price |
vars(card) |
in |
yes | true |
hasattr() |
Object.hasOwn() |
no | false |
in vars(card) |
The JavaScript way drops the loop. Object.keys() lists the card’s own fields in one call, as
list(vars(card)) does in Python, and Object.hasOwn() checks whether the card supplies its own
category instead of borrowing the default:
const defaults = { category: "print" };
const card = Object.create(defaults);
card.title = "Harbour";
card.price = 12;
const fields = Object.keys(card);
console.log(fields);
console.log(Object.hasOwn(card, "category"));
Run with node card.js, it prints the same two answers as the Python version:
[ 'title', 'price' ]
false
A for...in loop in other code should skip the inherited keys, with an if whose test is
Object.hasOwn(card, key). Older code calls a hasOwnProperty() method for the same check. For
your own data, reach for Object.keys() first. Finding a key along the link isn’t the same as
owning it.
Next up is the text lesson Setting a Property’s Flags. There, you give the card its ID and decide, flag by flag, whether that property can be changed, deleted, or listed.