Hello^^ Please Help me, i am troubled with iterator sections

const string1 = new String('custom');

 string1[Symbol.iterator] = function () {
    return {
      _first: true,

      next: function () {
       console.log(this._first) //why true?
        return this._first ?
          {value: 'bye', done: (this._first = false)} :  
          {done: true}
      }
    };
  };

 console.log(string1[Symbol.iterator]().next());  //return { value: 'bye', done: false }
 console.log(string1[Symbol.iterator]().next()); //return { value: 'bye', done: false }
for (let elem of string1) {
    console.log(elem)  
  }  //return bye

this is an example of Iterator section from MDN.

  1. _first variable is initialized to true
  2. so the code goes to here {value: ‘bye’, done: (this._first = false)}
    this._first variable is assigned to false.
  3. in the next iteration step, i don’t understand why console.log(this._first) print true.
  4. plus… i don’t understand , why “for of” loop only iterate only 1time. in the above, i found this._first is always true on every iteration .so done property is set to false on every iterations. but why “for of” statement only iterate 1 time.

any comment will help me a lot… i am totally stuck in this troubles and… ican;t find any solution online.

Do you have a link to this MDN article?

here you are

One more question. Were you working on some part of the freeCodeCamp curriculum that led you to researching about iterables? And if so, which part?

Understanding how to make custom iterables seems way out of the scope of what you need to know to complete freeCodeCamp (or to function as a web developer, for that matter).

this is my own study.
i studied functional programming section in Javascript.
in that section there are some subjects to define a function with the same functionality as filter, map…etc
and after that, i studied more about this. and i found i can make it by using iterators…
and that led me to MDN site to study iterators

sorry about making a question that is not the catergory of CODE CAMP tutorial.

i will delete this soon

  • _first variable is initialized to true

Correct, but in this case let’s call it a “property” since it’s on an object.

  • so the code goes to here {value: ‘bye’, done: (this._first = false)}
    this._first variable is assigned to false.

This code isn’t run until the next() is called and that branch of the ternary is reached. _first is still true when this object is defined.

  • in the next iteration step, i don’t understand why console.log(this._first) print true.

Because it was initialized to true, and hasn’t changed at this point.

  • plus… i don’t understand , why “for of” loop only iterate only 1time. in the above, i found this._first is always true on every iteration .so done property is set to false on every iterations. but why “for of” statement only iterate 1 time.

Because they’ve set the iterable to only iterate once with the value ‘bye’ then it’s done. Normally a string would return each of its characters in turn, but they’ve overwritten that functionality.

This is a contrived example, but the thing to understand is that any iterable item has an associated iterator object that is returned by the associated function. In the MDN example, the _first property in the iterator is used to short circuit iteration using the ternary. After the first iteration, the _first is indeed being set to false.

Your examples don’t show you that, because you call the function that returns an iterator object. Each time you do this, you are getting a new iterator object. This would not happen if you used one of the iterator constructs like the spread operator as they illustrate. Consider this:

const it = string1[Symbol.iterator]()
console.log(it.next())
console.log(it.next())
console.log(it.next())
1 Like