📜  生成器函数 fibonacci - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:52.833000             🧑  作者: Mango

代码示例1
// Fibonacci generator
function* fibonacci() {
  var a = 0;
  var b = 1;
  while (true) {
    yield a;
    a = b;
    b = a + b;
  }
}

// Instantiates the fibonacci generator
fib = fibonacci();

// gets first 10 numbers from the Fibonacci generator starting from 0
for (let i = 0; i < 10; i++) {
  console.log(i + ' => ' + fib.next().value);
}