This course has been updated! We now recommend you take the JavaScript: The Recent Parts course.

Check out a free preview of the full ES6: The Right Parts course:
The "Computed Generator Methods" Lesson is part of the full, ES6: The Right Parts course featured in this preview video. Here's what you'd learn in this lesson:

Kyle revisits the custom iterator code to show how it could be written using a generator instead of a custom iterator. This is possible because generators produce an iterator.

Get Unlimited Access Now

Transcript from the "Computed Generator Methods" Lesson

>> [MUSIC]

>> Kyle Simpson: So let's try to make this a little more concrete, let's come back to our example here. And let's think about how would we make this OBJ object iterable. Well I do need a symbol iterator here, right. But pretty much almost all of this stuff goes away.
>> Kyle Simpson: That's a good start.

[00:00:25] [LAUGH] Right. Now we want our symbol iterator to produce an iterator. What data type did we just find out automatically produces iterators? Generator. So let's do a concise computed generator method, okay. Now, this thing needs to simply spit out values. Here's what it's gonna do for var (i = this.start; i less than or equal to this.end,

>> Kyle Simpson: i++) Yield this stock values of [i].
>> Kyle Simpson: Gone.
>> Kyle Simpson: Think about what that's doing now. It's producing an iterator that is going to start at this .start location. Loop over its values array. And just yield out whatever it finds in that values array.
>> Kyle Simpson: I'm pretty sure if I was hard pressed I would have to tell you generator is either my favorite or my second favorite feature.

[00:01:58] It's a tie between generators and destructuring terms of my favorite, most powerful features. They have high learning curves, they have huge payoffs.
>> Kyle Simpson: More questions about a question, why is it called a generator? I don't know, that's actually not an invention of JavaScripts. We took inspiration from C#, and Python, and other languages which are called the generators.

[00:02:37] My on in my actual unofficial answer to that question is because they generate value. [INAUDIBLE] But I don't know if that's the real reason they're called generators.