From 2b59c05743c23f5c1fdb600ae3e21a8abd4fa8aa Mon Sep 17 00:00:00 2001 From: ruanyf Date: Mon, 30 Jun 2014 20:26:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9docs/generator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/class.md | 2 +- docs/generator.md | 2 ++ docs/reference.md | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/class.md b/docs/class.md index 81bdb77..870bf3d 100644 --- a/docs/class.md +++ b/docs/class.md @@ -94,7 +94,7 @@ function setHeader(element) { 上面代码中import关键字接受一个对象(用大括号表示),里面指定要从其他模块导入的变量。大括号里面的变量名,必须与被导入模块对外接口的名称相同。 -如果想为输入的属性或方法重新取一个名字,import语言要写成下面这样。 +如果想为输入的属性或方法重新取一个名字,import语句要写成下面这样。 ```javascript diff --git a/docs/generator.md b/docs/generator.md index 8c1dc09..c6322f1 100644 --- a/docs/generator.md +++ b/docs/generator.md @@ -97,6 +97,8 @@ g.next(true) // { value: 0, done: false } 上面代码先定义了一个可以无限运行的Generator函数f,如果next方法没有参数,每次运行到yield语句,变量reset的值总是undefined。当next方法带一个参数true时,当前的变量reset就被重置为这个参数(即true),因此i会等于-1,下一轮循环就会从-1开始递增。 +注意,由于next方法的参数表示上一个yield语句的返回值,所以第一次使用next方法时,不能带有参数。V8引擎直接忽略第一次使用next方法时的参数,只有从第二次使用next方法开始,参数才是有效的。 + ## 异步操作的应用 Generator函数的这种暂停执行的效果,意味着可以把异步操作写在yield语句里面,等到调用next方法时再往后执行。这实际上等同于不需要写回调函数了,因为异步操作的后续操作可以放在yield语句下面,反正要等到调用next方法时再执行。所以,Generator函数的一个重要实际意义就是用来处理异步操作,改写回调函数。 diff --git a/docs/reference.md b/docs/reference.md index 0a09502..046e0b1 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -35,6 +35,7 @@ - jmar777, [What's the Big Deal with Generators?](http://devsmash.com/blog/whats-the-big-deal-with-generators) - Marc Harter, [Generators in Node.js: Common Misconceptions and Three Good Use Cases](http://strongloop.com/strongblog/how-to-generators-node-js-yield-use-cases/): 讨论Generator函数的作用 - Axel Rauschmayer, [Iterators and generators in ECMAScript 6](http://www.2ality.com/2013/06/iterators-generators.html): 探讨Iterator和Generator的设计目的 +- StackOverflow, [ES6 yield : what happens to the arguments of the first call next()?](http://stackoverflow.com/questions/20977379/es6-yield-what-happens-to-the-arguments-of-the-first-call-next): 第一次使用next方法时不能带有参数 ## Promise对象