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/generator/next/index.html | 165 +++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/generator/next') diff --git a/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html b/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html new file mode 100644 index 0000000000..96ad60c35c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html @@ -0,0 +1,165 @@ +--- +title: Generator.prototype.next() +slug: Web/JavaScript/Reference/Global_Objects/Generator/next +tags: + - ECMAScript 2015 + - Generator + - JavaScript + - 原型 + - 参考 + - 方法 + - 生成器 +translation_of: Web/JavaScript/Reference/Global_Objects/Generator/next +--- +
{{JSRef}}
+ +

next() 方法返回一个包含属性 donevalue 的对象。该方法也可以通过接受一个参数用以向生成器传值。

+ +

语法

+ +
gen.next(value)
+ +

参数

+ +
+
value
+
向生成器传递的值.
+
+ +

返回值

+ +

返回的对象包含两个属性:

+ + + +

示例

+ +

使用 next()方法

+ +

下面的例子展示了一个简单的生成器, 以及调用 next 后方法的返回值:

+ +
function* gen() {
+  yield 1;
+  yield 2;
+  yield 3;
+}
+
+var g = gen(); // "Generator { }"
+g.next();      // "Object { value: 1, done: false }"
+g.next();      // "Object { value: 2, done: false }"
+g.next();      // "Object { value: 3, done: false }"
+g.next();      // "Object { value: undefined, done: true }"
+
+ +

向生成器传值

+ +

在此示例中,使用值调用next。 请注意,第一次调用没有记录任何内容,因为生成器最初没有产生任何结果。

+ +
function* gen() {
+  while(true) {
+    var value = yield null;
+    console.log(value);
+  }
+}
+
+var g = gen();
+g.next(1);
+// "{ value: null, done: false }"
+g.next(2);
+// 2
+// "{ value: null, done: false }"
+
+ +

规范

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-generator.prototype.next', 'Generator.prototype.next')}}{{Spec2('ES6')}}初始定义
{{SpecName('ESDraft', '#sec-generator.prototype.next', 'Generator.prototype.next')}}{{Spec2('ESDraft')}}草案
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}13{{CompatGeckoDesktop(26)}}{{CompatNo}}{{CompatVersionUnknown}}{{CompatSafari(10)}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support5.1{{CompatVersionUnknown}}{{CompatGeckoMobile(26)}}{{CompatUnknown}}{{CompatUnknown}}10
+
+
+ +

相关链接

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