Loading video player…

Checking Who Owns a Property

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:

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

Language: Program Output
[ '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:

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

Language: Program Output
[ '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.

00:00 In this lesson, you’ll 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 that default: every card gets "print" as its category.

00:15 Each instance gets its own title and price in its __init__ method. card.category finds "print" on the class. The export writes out each field a card supplies itself, so a loop walks vars(card), the instance’s own __dict__, and collects only the title and price.

00:34 The browser export needs the same split.

00:38 In JavaScript, the shared default goes in a plain object. A const declares a variable you can’t reassign; the object it holds can still change.

00:49 The braces make an object literal, written much like a dict. Object.create() then makes a new, empty card linked to the defaults. Dot assignment adds the title and price to the card itself, the way __init__ sets them on the instance.

01:05 So the card owns two keys, and the defaults hold the third.

01:10 console.log() prints a value. The card has no category of its own, so JavaScript follows the link and finds "print" on the defaults.

01:20 Python looks from the instance to its class, and JavaScript looks from the object to the object it’s linked to, with no class involved. That link is the object’s prototype, and prototypes get a course of their own later on.

01:36 Now port the loop. Square brackets start an empty array, and .push() adds to its end, like .append() in Python. for...in visits an object’s keys, the way for key in walks a dict.

01:50 There’s no vars() to port, since the card is already a plain object of keys. Each pass gets a fresh key, so const still fits.

01:59 The Python loop collected the title and price. What does looped hold in JavaScript? Your turn: pause the video and predict.

02:13 It holds the title, the price, and the category. The loop picked up the default, so the export would write it as the card’s own.

02:24 Why the extra key? for...in visits the card’s own keys first, 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.

02:39 That’s where the Python comparison stops: vars(card) stops at the instance, and for...in behaves more like dir(), which also looks at the class.

02:49 Object.keys() lists only the object’s own listable keys, as vars() does. It gives the title and price. On a plain literal with no link, the two agree, which is why they get mixed up.

03:04 Now check one key. In Python, hasattr() finds category through the class, while in on vars(card) says the instance doesn’t hold it.

03:14 JavaScript’s in operator works like hasattr(): it walks the link, so it gives true.

03:22 Watch the word: Python’s in on a dict checks only that dict, but JavaScript’s in looks along the link. Object.hasOwn() asks the card alone, like in on vars(card), and it gives false.

03:38 So the JavaScript way drops the loop. Object.keys() lists the card’s own fields in one call, as list(vars(card)) does in Python.

03:49 Object.hasOwn() checks whether the card supplies its own category. Both programs print the same two answers.

03:57 A for...in loop should skip inherited keys with an if, as in Python, but with the test in parentheses and the block in braces. Here, Object.hasOwn() is the test.

04:10 Older code calls a hasOwnProperty() method for the same check. For your own data, reach for Object.keys() first.

04:19 List own data with Object.keys(), and check a record’s own value with Object.hasOwn(). Finding a key along the link isn’t the same as owning it.

04:31 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.

Become a Member to join the conversation.