Symbol.iterator defines the default iterator for each object. This iterator can be used by… Of recycling

With the following properties: Writable, Enumerable, different (can be modified and deleted)

  • When an object needs to be iterated over, its@@iteratorMethods are called with no arguments, and the returned iterator is used to get the value to iterate over. Some built-in types have default iterator behavior, others [e.gObject】 does not.
  • Custom iterators (in case of specific requirements)

    var myIterable = {}     // Create a custom iterator if Object has no default iterator behavior
    myIterable[Symbol.iterator] = function* (){     // Create the Generator function implementation
        yield 1;
        yield 2;
        yield 3;
    };
    [...myIterable]     == [1,2,3] == [1,2,3]
    Copy the code
  • An iterative protocol is not a new built-in implementation or syntax, but a protocol. These protocols can be implemented by any object that follows certain conventions

    Iteration protocol is divided into two protocols: iterable protocol and iterator protocol

    • The iterable protocol allows JavaScript objects to define or customize their iterative behavior, for example, in a for… In the of structure, which values can be traversed. Some built-in types are also built-in iterables (String, Array, TypedArray, Map, Set) and are iterated by default. Syntax that requires iterables: for… of loops, expansion syntax, yield*, destruct assignment

      To be an iterable, the **@@iterator** method must be implemented. This means that the object (or an object in its prototype chain) must have a property whose key is @@iterator and which can be accessed through the constant symbol. iterator

    • The iterator protocol defines a standard way of producing a set of values, whether finite or infinite.

      An object can only be an iterator if it implements a next() method with the following semantics

      Properties: Next, value (a function that has no argument or one argument, returns an object that should have the following two properties: Done (Boolen) + value (Any JavaScript value returned by iterator, omitted if done is true)

      The next() method must return an object that should have two properties: done and value

      • String is a built-in iterable. The default iterator returns the code points of the String in turn.

        let someString = 'hi';
        console.log(typeof someString[Symbol.iterator]);    Next () returns two values: done and value.
        Copy the code
        let someString = 'hi';
        let iterator = someString[Symbol.iterator](); // Call its own object method
        iterator + ' ';
        console.log(iterator.next()); // Each time you execute the next() method, print a code pint
        console.log(iterator.next());
        console.log(iterator.next());
        Copy the code
      • Some of the built-in syntactic constructs — such as the expansion syntax — use the same iteration protocol for their internal implementations

        let someString = 'hi';
        console.log([...someString])    // Call for... Of carries out the loop output
        Copy the code
  • Simple iterator

    // An object can only be an iterator if it implements a next() method with the following semantic
    // A function with no arguments or one argument returns an object that should have the following two attributes
    // The above two sentences are interpreted as follows
    function makeIterator(array) {
      // Define a method with iterator structure syntax
      let nextIndex = 0;
      return {
        // There must be a return
        next: function () {
          return nextIndex < array.length
            ? {
                // Ternary operation to dynamically modify the done Boolean value
                value: array[nextIndex++], // Return value and done
                done: false,}, {done: true.// Change the Boolean value of done}; }}; }let it = makeIterator(['oh'.'ah']);
    console.log(it.next().value);
    console.log(it.next().value);
    console.log(it.next().value);
    Copy the code