nzakas / understandinges6

Content for the ebook "Understanding ECMAScript 6"
5.45k stars 796 forks source link

Potential Typo in Iterators & Generators chapter 8 #448

Closed ronen-e closed 3 years ago

ronen-e commented 4 years ago

In chapter 8 under the section "Iterables and for-of" - there seems to be a typo in the sentence

A for-of loop calls next() on an iterable each time the loop executes

However Iterable objects are not required to implement a next() method.

The for-of loop uses [Symbol.iterator]() on the iterable only to obtain the iterator object and then consume the iterator.

I think the correct terminology is to use iterator instead, like:

A for-of loop calls next() on an iterator each time the loop executes

nzakas commented 4 years ago

Yeah, that seems like a typo. Thanks.

nzakas commented 4 years ago

Okay, so this one is a bit tricky. The for-of loop technically works on iterables. It creates an iterator for the iterable and then calls next() on that. So the correct sentence might look like this:

A for-of loop works on an iterable and uses its Symbol.iterator property to retrieve an iterator. Then, the for-of loop calls next() on that iterator each time the loop executes and stores the value from the result object in a variable.

ronen-e commented 4 years ago

@nzakas 👍

btw, glad to hear you're feeling better