Iterator & for...of

for...of can tranverse data with any type

Refer: fibonacci

let fibonacci = {
  [Symbol.iterator]() {
    let pre = 0;
    let cur = 1;
    return {
      next() {
        [pre, cur] = [cur, pre + cur];
        return {
          done: false,
          value: cur,
        };
      },
    };
  },
};

for (let n of fibonacci) {
  if (n > 1000) {
    break;
  }
  console.log(n);
}

results matching ""

    No results matching ""