1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-27 20:32:21 +00:00

docs(generator): edit Generator

This commit is contained in:
ruanyf 2017-01-12 11:11:22 +08:00
parent a108bdaa0d
commit 2be52481a2
2 changed files with 4 additions and 4 deletions

View File

@ -221,7 +221,7 @@ g[Symbol.iterator]() === g
```javascript
function* f() {
for(var i=0; true; i++) {
for(var i = 0; true; i++) {
var reset = yield i;
if(reset) { i = -1; }
}
@ -234,9 +234,9 @@ g.next() // { value: 1, done: false }
g.next(true) // { value: 0, done: false }
```
上面代码先定义了一个可以无限运行的Generator函数`f`,如果`next`方法没有参数,每次运行到`yield`语句,变量`reset`的值总是`undefined`。当`next`方法带一个参数`true`时,上一次的变量`reset`就被重置为这个参数(即`true`),因此上一次的`i`会等于-1下一轮循环就会从-1开始递增。
上面代码先定义了一个可以无限运行的 Generator 函数`f`,如果`next`方法没有参数,每次运行到`yield`语句,变量`reset`的值总是`undefined`。当`next`方法带一个参数`true`时,变量`reset`就被重置为这个参数(即`true`),因此`i`会等于`-1`,下一轮循环就会从`-1`开始递增。
这个功能有很重要的语法意义。Generator函数从暂停状态到恢复运行它的上下文状态context是不变的。通过`next`方法的参数就有办法在Generator函数开始运行之后继续向函数体内部注入值。也就是说可以在Generator函数运行的不同阶段从外部向内部注入不同的值从而调整函数行为。
这个功能有很重要的语法意义。Generator 函数从暂停状态到恢复运行它的上下文状态context是不变的。通过`next`方法的参数,就有办法在 Generator 函数开始运行之后,继续向函数体内部注入值。也就是说,可以在 Generator 函数运行的不同阶段,从外部向内部注入不同的值,从而调整函数行为。
再看一个例子。

View File

@ -508,7 +508,7 @@ regex.test('ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ') // true
[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]
// 匹配各种文字的所有非字母的字符等同于Unicode版的\W
[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]
[^\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]
// 匹配所有的箭头字符
const regexArrows = /^\p{Block=Arrows}+$/u;