The GeneratorFunction
creates a new generator function
object. In JavaScript every generator function is actually a GeneratorFunction
object.
Note that GeneratorFunction
is not a global object. It could be obtained by evaluating the following code.
Object.getPrototypeOf(function*(){}).constructor
new GeneratorFunction ([arg1[, arg2[, ...argN]],] functionBody)
arg1, arg2, ... argN
x
", "theValue
", or "a,b
".functionBody
generator function
objects created with the GeneratorFunction
constructor are parsed when the function is created. This is less efficient than declaring a generator function with a function* expression
and calling it within your code, because such functions are parsed with the rest of the code.
All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.
Note: generator function
created with the GeneratorFunction
constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the GeneratorFunction
constructor was called. This is different from using eval
with code for a generator function expression.
Invoking the GeneratorFunction
constructor as a function (without using the new
operator) has the same effect as invoking it as a constructor.
GeneratorFunction.length
GeneratorFunction
constructor's length property whose value is 1.GeneratorFunction.prototype
GeneratorFunction
prototype objectGeneratorFunction.constructor
GeneratorFunction
.GeneratorFunction.prototype.prototype
%GeneratorPrototype%
.GeneratorFunction
instancesGeneratorFunction
instances inherit methods and properties from GeneratorFunction.prototype
. As with all constructors, you can change the constructor's prototype object to make changes to all GeneratorFunction
instances.
GeneratorFunction
constructorvar GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor var g = new GeneratorFunction('a', 'yield a * 2'); var iterator = g(10); console.log(iterator.next().value); // 20
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'GeneratorFunction' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'GeneratorFunction' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | Yes | Yes | 26 | No | Yes | ? |
prototype |
Yes | Yes | 26 | No | Yes | ? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | ? | 26 | No | ? | ? |
prototype |
Yes | Yes | ? | 26 | No | ? | ? |
function* function
function* expression
Function
function statement
function expression
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction