From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../reference/operators/function_star_/index.html | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/operators/function_star_/index.html (limited to 'files/zh-cn/web/javascript/reference/operators/function_star_') diff --git a/files/zh-cn/web/javascript/reference/operators/function_star_/index.html b/files/zh-cn/web/javascript/reference/operators/function_star_/index.html new file mode 100644 index 0000000000..1f607de5d9 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/function_star_/index.html @@ -0,0 +1,89 @@ +--- +title: function* 表达式 +slug: Web/JavaScript/Reference/Operators/function* +tags: + - ECMAScript 2015 + - Function + - Iterator + - JavaScript + - Operator + - Primary Expression +translation_of: Web/JavaScript/Reference/Operators/function* +--- +
{{jsSidebar("Operators")}}
+ +

function*关键字可以在表达式内部定义一个生成器函数。

+ +

{{EmbedInteractiveExample("pages/js/expressions-functionasteriskexpression.html")}}

+ +

语法

+ +
function* [name]([param1[, param2[, ..., paramN]]]) {
+   statements
+}
+ +

参数

+ +
+
name
+
函数名。在声明匿名函数时可以省略。函数名称只是函数体中的一个本地变量。
+
paramN
+
传入函数的一个参数名。一个函数最多有 255 个参数。
+
statements
+
函数体。
+
+ +

描述

+ +

function*表达式和{{jsxref('Statements/function*', 'function* 声明')}}比较相似,并具有几乎相同的语法。function*表达式和function*声明之间主要区别就是函数名,即在创建匿名函数时,function*表达式可以省略函数名。阅读{{jsxref('Function', '函数')}}章节了解更多信息。

+ +

示例

+ +

下面的示例定义了一个未命名的生成器函数并把它赋值给x。函数产出它的传入参数的平方:

+ +
var x = function*(y) {
+   yield y * y;
+};
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#', 'function*')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#', 'function*')}}{{Spec2('ESDraft')}}
+ +

浏览器兼容

+ + + +

{{Compat("javascript.operators.function_star")}}

+ +

相关链接

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