This is the 8th day of my participation in the August More Text Challenge

Iterator

concept

  • The Iterator (traversal) : Is a kind of unityMechanism of the interfaceFor handling all the different data structures.
  • Iterator: An interface that provides a unified access mechanism for various data structures.
  • Any data structure can be iterated by deploying the Iterator interface.
  • Iterator does:
    1. Provides a unified and easy access interface for various data structures.
    2. Enables the members of a data structure to be arranged in some order.
    3. Created a new traversal command for ES6. The Iterator interface is primarily for… Of consumption

Iterator iterates through the process

  1. Creates a pointer object that points to the start of the current data structure. In other words,The traverser object is essentially a pointer object.
  2. The first call to the next method of the pointer object points to the first member of the data structure.
  3. The next call to the next method of the pointer object points to the second member of the data structure.
  4. Call the next method of the pointer object until it points to the end of the data structure.
  • Note: Each time the next method is called, all return information about the current member of the data structure. namelyReturns an object containing both value and done properties. Value is the value of the current member, and done is a Boolean value indicating whether the traversal is complete.

An iterator function:

function Iterator(array) {
    let nextIndex = 0;
    return {
        next: function() {
            return nextIndex < array.length ? 
            {value: array[nextIndex]} : 
            {done: true}}}}Copy the code

TypeScript writing

interface Iterable {
  [Symbol.iterator]() : Iterator,
}

interfaceIterator { next(value? :any) : IterationResult,
}

interface IterationResult {
  value: any.done: boolean,}Copy the code

The default Iterator interface

  • When using the for... When the of loop iterates over some data structure, the loop automatically looks for the Iterator interface.
  • A data structure is said to be “iterable” whenever the Iterator interface is deployed.
  • ES6 regulation,The default Iterator interface is deployed in the symbol. Iterator property of the data structureIn other words,A data structure only needs to have the Symbol. Iterator attributeIs considered “iterable.”
  • Symbol.iteratorThe property itself is a function, which is the default traverser generator for the current data structure. Executing this function returns a traverser. As far as attribute names are concernedSymbol.iterator, which is an expression that returnsSymbolThe object’siteratorProperty, which is a predefined special value of type Symbol,So we put it in square brackets.
const obj = {
  [Symbol.iterator] : function () {
    return {
      next: function () {
        return {
          value: 1.done: true}; }}; }};Copy the code

A data structure that has an Iterator interface natively:

  • Array
  • Map
  • Set
  • String
  • TypedArray
  • The arguments object for the function
  • The NodeList object

Objects don’t have a default Iterator, so you can’t use for… Of traversal.

The context in which the Iterator interface is called

  • Deconstruction assignment
  • Extended operator
  • yield*
  • Other occasions:
    • for… of
    • Array.from()
    • Map(),Set(),WeakMap(),WeakSet()
    • Promise.all()
    • Promise.race()

methods

Methods for traversing an object:

  • Next (): Must deploy,
  • Return (): optional,
  • Throw () : optional