mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-25 19:22:21 +00:00
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
This commit is contained in:
commit
b178113795
@ -263,7 +263,7 @@ b.next(13) // { value:42, done:true }
|
||||
|
||||
如果向`next`方法提供参数,返回结果就完全不一样了。上面代码第一次调用`b`的`next`方法时,返回`x+1`的值`6`;第二次调用`next`方法,将上一次`yield`表达式的值设为`12`,因此`y`等于`24`,返回`y / 3`的值`8`;第三次调用`next`方法,将上一次`yield`表达式的值设为`13`,因此`z`等于`13`,这时`x`等于`5`,`y`等于`24`,所以`return`语句的值等于`42`。
|
||||
|
||||
注意,由于`next`方法的参数表示上一个`yield`表达式的返回值,所以第一次使用`next`方法时,不能带有参数。V8 引擎直接忽略第一次使用`next`方法时的参数,只有从第二次使用`next`方法开始,参数才是有效的。从语义上讲,第一个`next`方法用来启动遍历器对象,所以不用带有参数。
|
||||
注意,由于`next`方法的参数表示上一个`yield`表达式的返回值,所以在第一次使用`next`方法时,传递参数是无效的。V8 引擎直接忽略第一次使用`next`方法时的参数,只有从第二次使用`next`方法开始,参数才是有效的。从语义上讲,第一个`next`方法用来启动遍历器对象,所以不用带有参数。
|
||||
|
||||
如果想要第一次调用`next`方法时,就能够输入值,可以在 Generator 函数外面再包一层。
|
||||
|
||||
|
@ -716,7 +716,7 @@ for (let e in es6) {
|
||||
for (let e of es6) {
|
||||
console.log(e);
|
||||
}
|
||||
// TypeError: es6 is not iterable
|
||||
// TypeError: es6[Symbol.iterator] is not a function
|
||||
```
|
||||
|
||||
上面代码表示,对于普通的对象,`for...in`循环可以遍历键名,`for...of`循环会报错。
|
||||
|
Loading…
x
Reference in New Issue
Block a user