MDN wants to learn about developers like you: https://qsurvey.mozilla.com/s3/MDN-dev-survey

这篇翻译不完整。请帮忙从英语翻译这篇文章

Summary 概要

GeneratorFunction构造器生成新的generator function 对象。在JavaScript中,生成器函数实际上都是GeneratorFunction的实例对象

注意到,GeneratorFunction并不是一个全局对象。可以通过下面的代码获取。

Object.getPrototypeOf(function*(){}).constructor

Syntax 语法

new GeneratorFunction ([arg1[, arg2[, ...argN]],] functionBody)

参数

arg1, arg2, ... argN
Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "x", "theValue", or "a,b".
函数体
一个包含多条表示JavaScript函数体语句的字符串。

Description 描述

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.

Properties 属性

GeneratorFunction.length
The GeneratorFunction constructor's length property whose value is 1.
GeneratorFunction.prototype
Allows the addition of properties to all generator function objects.

GeneratorFunction prototype object

Properties

GeneratorFunction.constructor
The initial value is GeneratorFunction.
GeneratorFunction.prototype.prototype
The value is %GeneratorPrototype%.

GeneratorFunction 实例

GeneratorFunction 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.

Examples 例子

Creating a generator function from a GeneratorFunction constructor

var GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor
var g = new GeneratorFunction("a", "yield a * 2");
var iterator = g(10);
console.log(iterator.next().value); // 20

Specifications 规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
GeneratorFunction
Standard Initial definition.

Browser compatibility 浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) 26 (26) ? ? ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? (Yes) 26 (26) ? ? ?

See also 另见

文档标签和贡献者

 此页面的贡献者: lanezhao, webery, Cendy
 最后编辑者: lanezhao,