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

Listing Every Kind of Own Key

Listing Every Kind of Own Key

In this lesson, you find out which listing shows which of the proof card’s own keys, and which one shows them all. The card has a title and a price, the read-only id you set with its flags, and a tag saved under the SOURCE symbol, card[SOURCE] = "proof desk". Before the card goes into a customer’s export, the desk needs to know which keys each tool sees, its own tag included.

In Python, vars(card) lists every attribute on the instance, and json.dumps(vars(card)) writes them all out, _id included: the underscore is only a naming convention. In JavaScript, Object.keys(card) and JSON.stringify(card) give only the title and the price. The id was made non-enumerable, but the symbol tag, added with an ordinary assignment, is missing too.

Each listing asks two separate questions about a key: is it enumerable, and is the key a string or a symbol? On the card, title and price are enumerable strings, id is a string that isn’t enumerable, and the tag is an enumerable symbol. Object.keys() and JSON.stringify() want a yes to both questions. Python’s vars() asks neither: Python has no enumerable flag, and every attribute name on the instance is a string.

Here’s the whole listing.js, with one listing per line from line 9 on:

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 In this lesson, you’ll find out which listing shows which of the proof card’s own keys, and which one shows them all. The card has a title and a price, the read-only id you set with its flags, and the source tag you saved under a symbol.

00:15 Before the card goes into a customer’s export, the desk needs to know which keys each tool sees, its own tag included. In Python, vars() lists every attribute on the instance, and json.dumps() writes them all out.

00:31 Even _id shows up, because the underscore is only a naming convention. The JavaScript lines ask Object.keys() and JSON.stringify() instead.

00:43 Both give only the title and the price. The id was made non-enumerable, but the tag was added with an ordinary assignment, and it’s missing too.

00:54 Each listing asks two separate questions about a key. First, is it enumerable? Second, is the key a string or a symbol? Sort the card’s keys by those two questions.

01:09 The title and the price are enumerable strings. The id is a string that isn’t enumerable. The tag is an enumerable symbol, and this card has nothing in the last corner.

01:21 Object.keys() and JSON.stringify() want a yes to both questions. The id fails the first, and the tag fails the second. Python’s vars() asks neither: Python has no enumerable flag, and every attribute name on the instance is a string.

01:41 For every string key, enumerable or not, add Object.getOwnPropertyNames(), the listing that found the id when you set its flags. Run the file again, and it gives the title, the price, and the id.

01:55 So non-enumerable keeps the id out of Object.keys(), not out of sight. It reads the whole string column, and that’s all. The symbol still isn’t there.

02:08 For the symbol column, add Object.getOwnPropertySymbols() to the file. It returns an array of the card’s own symbol keys, enumerable or not: here, the one source symbol.

02:22 A symbol prints as the word Symbol, with its description in parentheses. A non-enumerable symbol would land in the empty cell below it too. Watch out: like Python’s underscore, a symbol key is a signal, not a lock: anyone holding the card can list it this way, and private state needs private class fields, a topic for later.

02:46 When you want every own key, Reflect.ownKeys() lists them all. Reflect is a built-in object of low-level helpers, and ownKeys() is the only one you need here.

02:57 It gives all four own keys: the title, the price, the id, and the source symbol.

03:04 The order is fixed. String keys come first, in the order they were added, and symbols come after them. Among the strings, non-negative integer keys, such as 1 and 2, lead the rest, in ascending order.

03:19 This is the nearest match to Python’s list(vars(card)): every own key. The match stops at the keys: Python’s are all strings, and this list mixes in symbols.

03:31 Neither one lists the category, which the card inherits.

03:37 Now copy the card with spread, and log the copy. Which of the four keys does the copy keep? Your turn: pause the video and predict.

03:52 The copy keeps the title, the price, and the source tag, but not the id. Spread asks only the first question: is the key enumerable? So it copies an enumerable symbol too, and console.log() shows that symbol key as well.

04:09 Copying follows the enumerable question, while JSON also wants a string.

04:16 So pick the listing by the question you’re asking. A saved symbol keeps bookkeeping out of Object.keys() and JSON, without hiding it from anyone.

04:25 To check every own key, use Reflect.ownKeys(), and for a public export, choose the fields on purpose.

04:34 Next up is the exercise Recover the Proof Desk Tag. There, you’ll read a finishing desk’s tag back off a card, and keep that bookkeeping out of the exported JSON.

Become a Member to join the conversation.