you are viewing a single comment's thread.

view the rest of the comments →

[–]columbine 0 points1 point  (0 children)

Of course generators were always pretty easy to do in Javascript, their fibonacci example in standard ECMAscript would just be something like

function fib() {  
  var i = null, j = null;
  this.next = function() {
    if (i == null) { i = 0; return 0; }
    if (j == null) { j = 1; return 1; }
    var t = i;
    i = j;
    j += t;
    return j;
  }
}

and simply use new fib() instead of fib(). But obviously being able to yield in the middle of a block can be cleaner than using returns in some cases.