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.

The Iterator pattern

The Iterator pattern - Node.js Tutorial

From the course: Node.js: Design Patterns

Start my 1-month free trial

The Iterator pattern

- [Instructor] The iterator is a very common design pattern. It gives us a uniform interface for interacting with lists, collections, arrays, or any type of aggregate object. Iterators are designed to work with collections of data. They give us a clear way to access the first data record, and then they give us a way to iterate through the rest of the rows within our data set. It doesn't matter what type of data you're working with, iterators give us a clear way to iterate through any collection. The Gang of Four defines the intent of the iterator as providing a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Let's take a look at some code. Within the exercise files, under chapter four, lesson six within the start folder, we have a couple of files already set up. We have an inventoryitem.js, which has the class definition for an inventory item. Inventory items just have a name and a price, and they also have a writeline…

Contents