js generator

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);

  

posted @ 2017-05-03 21:16  ax=null  阅读(138)  评论(0编辑  收藏  举报