Skip to content

Commit cf0dc09

Browse files
committed
edit generator/yield*
1 parent 2b10934 commit cf0dc09

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/class.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class之间可以通过extends关键字,实现继承。
3434
class ColorPoint extends Point {
3535

3636
constructor(x, y, color) {
37-
super(x, y); // same as super.constructor(x, y)
37+
super(x, y); // 等同于super.constructor(x, y)
3838
this.color = color;
3939
}
4040

@@ -46,7 +46,7 @@ class ColorPoint extends Point {
4646

4747
```
4848

49-
上面代码定义了一个ColorPoint类,该类通过extends关键字,继承了Point类的所有属性和方法。在constructor方法内,super就指代父类Point。
49+
上面代码定义了一个ColorPoint类,该类通过extends关键字,继承了Point类的所有属性和方法。在constructor方法内,super就指代父类Point;在toString方法内,`super()`表示对父类求值,由于此处需要字符串,所以会自动调用父类的toString方法
5050

5151
## Module的基本用法
5252

docs/generator.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,47 @@ for(let value of delegatingIterator) {
178178
// "Ok, bye."
179179

180180
```
181+
182+
上面代码中,delegatingIterator是代理者,delegatedIterator是被代理者。由于`yield* delegatedIterator`语句得到的值,是一个遍历器,所以要用星号表示。
183+
184+
下面是一个稍微复杂的例子,使用yield*语句遍历二叉树。
185+
186+
```javascript
187+
188+
// 下面是二叉树的构造函数,
189+
// 三个参数分别是左树、当前节点和右树
190+
function Tree(left, label, right) {
191+
this.left = left;
192+
this.label = label;
193+
this.right = right;
194+
}
195+
196+
// 下面是中序(inorder)遍历函数。
197+
// 由于返回的是一个遍历器,所以要用generator函数。
198+
// 函数体内采用递归算法,所以左树和右树要用yield*遍历
199+
function* inorder(t) {
200+
if (t) {
201+
yield* inorder(t.left);
202+
yield t.label;
203+
yield* inorder(t.right);
204+
}
205+
}
206+
207+
// 下面生成二叉树
208+
function make(array) {
209+
// 判断是否为叶节点
210+
if (array.length == 1) return new Tree(null, array[0], null);
211+
return new Tree(make(array[0]), array[1], make(array[2]));
212+
}
213+
let tree = make([[['a'], 'b', ['c']], 'd', [['e'], 'f', ['g']]]);
214+
215+
// 遍历二叉树
216+
var result = [];
217+
for (let node of inorder(tree)) {
218+
result.push(node);
219+
}
220+
221+
result
222+
// ['a', 'b', 'c', 'd', 'e', 'f', 'g']
223+
224+
```

docs/reference.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 参考链接
22

3+
## 官方文件
4+
35
- [ECMAScript 6 Language Specification](http://people.mozilla.org/~jorendorff/es6-draft.html): 语言规格草案
46
- [harmony:proposals](http://wiki.ecmascript.org/doku.php?id=harmony:proposals): ES6的各种提案
57

0 commit comments

Comments
 (0)