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

Iterating Over a Kit

Iterating Over a Kit

In this lesson, you make a kit loopable in JavaScript, the way __iter__ does in Python. The checkout desk lists a kit’s tools every time it’s loaned. In Python, the kit’s __iter__ method returns iter(self.tools), so list() can walk any kit, as often as you ask, without knowing the tools sit in a list. JavaScript has no dunder names, so a loop needs another hook.

A symbol is a key value that no string can equal, and brackets put a computed key in a class. Symbol.iterator is the one built-in symbol whose method loops look for. Watch out: the key is the symbol itself, so a string spelled "Symbol.iterator" is a different key, and no loop looks for it.

Suppose the port returns the tools array itself. Then it spreads the kit. Spread, three dots, works like Python’s [*drilling]:

Language: JavaScript
class Kit {
  constructor(code, tools) {
    this.code = code;
    this.tools = [...tools];
  }

  [Symbol.iterator]() {
    return this.tools;
  }
}

const drilling = new Kit("KIT-17", ["drill", "bits"]);
console.log([...drilling]);

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 make a kit loopable in JavaScript, the way __iter__ does in Python. The checkout desk lists a kit’s tools every time it’s loaned.

00:09 In Python, the kit’s __iter__ method returns iter(self.tools), so list() can walk any kit without knowing the tools sit in a list.

00:19 Ask twice, and it prints drill and bits both times.

00:24 The JavaScript port so far keeps the code and its own copy of the tools, and logs the array. But JavaScript has no dunder names. So where does a loop look for its hook?

00:37 A symbol is a key value that no string can equal, and brackets put a computed key in a class. Symbol.iterator is the one built-in symbol whose method loops look for.

00:49 Watch out: the key is the symbol itself, so a string spelled “Symbol.iterator” is a different key, and no loop looks for it. Suppose the port returns the tools array itself.

01:02 Then it spreads the kit, three dots, like Python’s [*drilling], and logs the result. What does this line log? Your turn: pause the video and predict.

01:19 It throws a TypeError: drilling is not iterable.

01:26 That’s confusing, because the kit has the method. Spread looks up Symbol.iterator on the kit, and finds the method on Kit.prototype, through the chain.

01:37 It calls the method. Then it calls .next(), JavaScript’s __next__, on whatever came back, and an array has none. An array is iterable: it has a Symbol.iterator method.

01:51 It isn’t an iterator, the object with a next method that walks it. Python draws the same line. Return the list itself from __iter__, and Python raises a TypeError too: it must return an iterator, not a list.

02:08 JavaScript’s version of iter(self.tools) is the array’s .values() method. It returns a fresh iterator over the tools, and the kit’s method hands it back.

02:19 Now both spreads log drill and bits.

02:23 That’s how JavaScript developers write it: delegate to the array you hold, and let its iterator do the stepping. And for...of uses the same hook, so a loop over the kit visits drill, then bits.

02:37 Call the hook yourself, and keep the cursor it returns. Each .next() call returns a result object with two properties. value holds the tool: drill, then bits.

02:49 And done stays false while there’s a tool to give. The third call finds nothing left: value is undefined, and done is true.

02:59 Python’s next() gives back the tool itself. Here the analogy stops: at the end, Python raises StopIteration, where JavaScript returns one more result, marked done.

03:12 Spread and for...of call .next() until they see it.

03:17 So why did both spreads list both tools? Each spread calls the kit’s method again, and each call makes a new iterator that starts at drill. You know this from Python.

03:29 Cache iter(self.tools) in __init__, to avoid making one per loop, and the second list comes back empty.

03:37 A JavaScript iterator runs dry the same way. The constructor stores it as this.cursor, and the second spread logs an empty array. So build the iterator inside the method, one per loop.

03:52 So the hook is a method under the symbol, in brackets. It returns an iterator, whose next method gives a value and a done flag. For a kit, the array’s values method hands back a fresh one for every loop.

04:07 Next up is the text lesson Choosing a Protocol’s Meaning. There, you’ll see that Python’s protocols map to different JavaScript hooks rather than one table of dunders, and when a hook is worth customizing at all.

Become a Member to join the conversation.