0

I am exploring new features introduced in Javascript 1.7. In generators, I came across the following code and could not understand the difference an asterisk (*) would make to the function declaration. Link : https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7#Generators

With asterisk, JSFiddle

Only Firefox supports Javascript 1.7. Open this fiddle in Firefox

function *fib(){
    var i=0,j=1;
    while(true){
        yield i;
        var t=i;
        i=j;
        j+=t;
    }
} 

var g=fib();
console.log(g.next().value);//0
console.log(g.next().value);//1
console.log(g.next().value);//1

Without asterisk,

function fib(){
    var i=0,j=1;
    while(true){
        yield i;
        var t=i;
        i=j;
        j+=t;
    }
}

var g=fib();
console.log(g.next().value);//undefined
console.log(g.next().value);//undefined
console.log(g.next().value);//undefined
Ashwin Aggarwal
  • 589
  • 7
  • 20
  • 2
    Well, since the folks here didn't quite get that you asked about two different generator types, and not the difference between functions and ecma-6 generators, it is unfortunate this was closed... The short answer would have been that the non-standard, non-asterisk version was introduced in mozilla's engine long before there was an ecma-6 spec draft that dictated the asterisk and certain other behavioral differences (e.g. `.next()` vs. `.next().value`). Consider the mozilla-only version the prototype that started the ecma-6 spec discussion on generators. – nmaier Jun 06 '14 at 07:50
  • You understood my query. Thanks @nmaier for your brief explanation. – Ashwin Aggarwal Jun 07 '14 at 09:36

0 Answers0