js generator

it2026-04-18  5

function* fib() { let previous = 0; let current = 1; while (true) { yield current; const next = current + previous; previous = current; current = next; } } /* output: 1 1 2 3 5 8*/ const g = fib(); console.log(g.next().value); console.log(g.next().value); console.log(g.next().value); console.log(g.next().value); console.log(g.next().value); console.log(g.next().value);

  

转载于:https://www.cnblogs.com/ax-null/p/6803932.html

相关资源:JS Generator 函数的含义与用法实例总结
最新回复(0)