From the course: Node.js: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implementing iterators

Implementing iterators - Node.js Tutorial

From the course: Node.js: Design Patterns

Start my 1-month free trial

Implementing iterators

- [Instructor] In the last lesson we introduced the iterator pattern and modified our code to use the pattern. In this lesson, we're going to build the iterator class and make our application work. So inside of the exercise files for Chapter Four, Chapter Four Lesson 7 and within the start folder I'm going to go ahead and create a new file that we're going to call iterator.js. And for the iterator, I'm just going to create a class called Iterator and I'm going to make sure I export that class and I will come up within the class and then give it a constructor and when we create a new instance of the iterator, we need to collect an array. I'm just going to call those Items and we will go ahead and add that to our constructor arguments. We'll also default it as an empty array, so if we accidentally do not provide anything, we will have an empty iterator. Now, the methods that we need to add to this are first, last, next and prev. So the methods that we use for iterating to an object…

Contents