From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/generatorfunction/index.html | 113 +++++++++++++++++++++ .../generatorfunction/prototype/index.html | 64 ++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/generatorfunction') diff --git a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html new file mode 100644 index 0000000000..ab93243b97 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html @@ -0,0 +1,113 @@ +--- +title: GeneratorFunction +slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +tags: + - Constructor + - ECMAScript 2015 + - GeneratorFunction + - Iterator + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +--- +
{{JSRef("Global_Objects", "生成器函数")}}
+ +

GeneratorFunction构造器生成新的{{jsxref("Statements/function*", "生成器函数")}} 对象。在JavaScript中,生成器函数实际上都是GeneratorFunction的实例对象。

+ +

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

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

语法

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

参数

+ +
+
arg1, arg2, ... argN
+
函数使用的名称作为形式参数名称。每个必须是一个字符串,对应于一个有效的JavaScript标识符或这样的字符串的列表,用逗号分隔;如“x”,“theValue”或“a,b”。
+
functionBody
+
一个包含多条表示JavaScript函数体语句的字符串。
+
+ +

描述

+ +

当创建函数时,将使用GeneratorFunction构造函数创建的{{jsxref("Statements/function*", "生成器函数")}}对象进行解析。这比使用{{jsxref("Statements/function*", "function* 表达式")}} 声明生成器函数效率更低,并且在代码中调用它,因为这些函数与其余的代码一起被解析。

+ +

传递给函数的所有参数按照它们被传递的顺序被视为要创建的函数中参数的标识符的名称。

+ +
+

提示:使用GeneratorFunction构造函数创建的{{jsxref("Statements/function*", "生成器函数")}}不会为其创建上下文创建闭包;它们始终在全局范围内创建。当运行它们时,它们只能访问自己的本地变量和全局变量,而不是从GeneratorFunction构造函数调用的范围的变量。这与使用{{jsxref("Global_Objects/eval", "eval")}}与生成函数表达式的代码不同。

+
+ +

GeneratorFunction构造函数调用为函数(不使用new运算符)与将其作为构造函数调用的效果相同。

+ +

属性

+ +
+
GeneratorFunction.length
+
GeneratorFunction构造函数的 length 属性值为 1。
+
{{jsxref("GeneratorFunction.prototype")}}
+
允许向所有生成器函数对象添加属性。
+
+ +

GeneratorFunction 原型对象

+ +

属性

+ +
{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype', 'Properties')}}
+ +

GeneratorFunction 实例

+ +

GeneratorFunction实例从{{jsxref("GeneratorFunction.prototype")}}继承方法和属性。与所有构造函数一样,你可以更改构造函数的原型对象以对所有GeneratorFunction实例进行更改。

+ +

示例

+ +

GeneratorFunction构造函数创建一个生成器函数

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

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-generatorfunction-objects', 'GeneratorFunction')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-generatorfunction-objects', 'GeneratorFunction')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.GeneratorFunction")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html new file mode 100644 index 0000000000..c723725c05 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html @@ -0,0 +1,64 @@ +--- +title: GeneratorFunction.prototype +slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype +tags: + - ECMAScript 2015 + - GeneratorFunction + - Iterator + - JavaScript + - Property + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +--- +
{{JSRef}}
+ +

GeneratorFunction.prototype属性是{{jsxref("GeneratorFunction")}}的原型对象。

+ +

描述

+ +

{{jsxref("GeneratorFunction")}} 的实例对象都继承于 GeneratorFunction.prototype. GeneratorFunction.prototype 不能被修改。

+ +

属性

+ +
+
GeneratorFunction.constructor
+
初始值是 {{jsxref("GeneratorFunction")}}.
+
GeneratorFunction.prototype.prototype
+
值是 %GeneratorPrototype%.
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-generatorfunction.prototype', 'GeneratorFunction.prototype')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-generatorfunction.prototype', 'GeneratorFunction.prototype')}}{{Spec2('ESDraft')}} 
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.GeneratorFunction.prototype")}}

+ +

相关链接

+ + -- cgit v1.2.3-54-g00ecf