实现斐波那契数列之es5、es6

es5实现斐波拉契函数数列:

es6实现斐波拉契函数数列:

function* fibonacci() {
  let [prev, curr] = [0, 1];
  for (;;) {
    yield curr;
    [prev, curr] = [curr, prev + curr];
  }
}

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

参考链接:https://www.cnblogs.com/bitkuang/p/9711304.html

     http://es6.ruanyifeng.com/#docs/generator

你可能感兴趣的:(实现斐波那契数列之es5、es6)