GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaScript-Design-Patterns/GoF/classic/Behavioral/Iterator at master · irynaO/JavaScript-Design-Patterns

Expand file tree

/

Iterator

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

Iterator

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
CoffeeScript
CoffeeScript
 
 
ECMAScript
ECMAScript
 
 
TypeScript
TypeScript
 
 
Iterator.dia
Iterator.dia
 
 
Iterator.png
Iterator.png
 
 
README.md
README.md
 
 

README.md

Outline

Synopsis

I am at the library and I would like to see the list of all available books.

Problem

There is generally a huge amount of books in a library, so it is great to have a catalog that allows you to have a quick overview of all available references. This catalog may be a bit special and could contain a one-page presentation for each book.

In Javascript, we could use a simple loop to get the list of all books. But in fact, this would not be so convenient because each data structure and entity may have its own particularities when it comes to iteration. For instance, we know we cannot iterate through an array literal like we iterate through an object literal (array-like objects could be considered as an exception). Therefore this is generally a good idea to simplify the work to iterate through a collection...

Solution

Iterator is the design pattern we need for this. To implement this pattern, we could have:

  • An abstract representation of a collection (abstract class or interface)
  • An abstract representation of a catalog (abstract class or interface)
  • A concrete collection like a library (which is a collection of books)
  • A concrete catalog like a library catalog (which references all books)

In this particular case, a collection is an iterable object whereas a catalog is an iterator object.

Iterator (classic)

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.