Last active
March 15, 2021 07:51
-
-
Save NovoManu/80e5062b319deb574cc30c69c58e7f3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fibonacci = { | |
// Iterated objects must implement [Symbol.iterator] method | |
// which returns object with next method | |
[Symbol.iterator]() { | |
let pre = 0, cur = 1 | |
return { | |
next() { | |
[pre, cur] = [cur, pre + cur] | |
return { done: false, value: cur } | |
} | |
} | |
} | |
} | |
// It's possible to iterate with for/of loop | |
for (const n of fibonacci) { | |
if (n > 100) break | |
console.log(n) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment