0

Using normal ECMAScript you can do something like,

function f () { console.log(this.constructor.name); }
new f() // outputs `f`

However, a slight modification,

function* f () { console.log(this.constructor.name); }
var g = new f();
g.next() // outputs `GeneratorFunctionPrototype`

Is there anyway to get the generator's name (f)?

user157251
  • 64,489
  • 38
  • 208
  • 350
  • I don't think the function has to have a name, although one might argue the name (inside) is "f". Although not a strong argument, `Function.prototype.name` is non-standard and `constructor.name` is really just an artifact of the new object - not the function. – user2864740 Jan 22 '14 at 18:22
  • Do you have to use `this`? – Knu Mar 24 '15 at 11:56

1 Answers1

0

You could use the property callee of the arguments object. However note that calleewas removed from the ES5 strict mode (no idea about ES6 unfortunately).

So you'd get something like:

function* f () {console.log(arguments.callee.name);}
var g = new f();
g.next() // outputs `f`
Loamhoof
  • 8,163
  • 24
  • 29