Skip to content

Instantly share code, notes, and snippets.

@NovoManu
Last active March 15, 2021 07:51
Show Gist options
  • Save NovoManu/80e5062b319deb574cc30c69c58e7f3c to your computer and use it in GitHub Desktop.
Save NovoManu/80e5062b319deb574cc30c69c58e7f3c to your computer and use it in GitHub Desktop.
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