1

What is the difference between generator functions created with function and function*

function a(i){
    for(;i>0;i--){
        yield i*i;
    }
}
function *b(i){
    for(;i>0;i--){
        yield i*i*i;
    }
}
KOLANICH
  • 2,234
  • 2
  • 16
  • 19
  • I've seen like this question, but currently couldn't find... – Bhojendra Rauniyar Jan 16 '15 at 18:22
  • Or this one: [What purpose of asterisk (*) in ES6 generator functions](http://stackoverflow.com/questions/27778105/what-purpose-of-asterisk-in-es6-generator-functions) –  Jan 16 '15 at 18:25

1 Answers1

0

Generators created with function are part of earlier ES6 draft.

//They have differrent prototypes
console.log(a.prototype.constructor.constructor,b.prototype.constructor.constructor);//function Function() function GeneratorFunction()
let a1=a(10);
let b1=b(10);
//both create generators...
console.log(a1,b1);//Generator {  } Generator {  }
//but different generators: one returns value, another returns an object of special format
console.log(a1.next(),b1.next());//100 Object { value: 1000, done: false }
for(let a2 of a1)console.log(a2);
for(let b2 of b1)console.log(b2);
//They are equal when used in for ... of.
KOLANICH
  • 2,234
  • 2
  • 16
  • 19