K
K
KononovD2019-03-11 19:46:12
JavaScript
KononovD, 2019-03-11 19:46:12

Symbol and for of overload in js?

Hello.
Do I understand correctly that the following code:

let obj = {
  to: 10,
  [Symbol.iterator]: function () {
    let curr = 0;
    let stop = this.to;

    return {
      next() {
        if (curr <= stop)
          return {
            done: false,
            value: curr++
          }
        else
          return {
            done: true
          }
      }
    }
  }
}

for (let num of obj) {
  console.log(num);
}

is it actually an overload of the for of loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-03-11
@KononovD

Well, you can call it that. In fact, this is the use of language tools to pass through the iterator. In JS, there is no formal concept of "overloading" but informally - call it whatever you like.
What exactly is the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question