-
Notifications
You must be signed in to change notification settings - Fork 0
/
testGenrator.js
50 lines (42 loc) · 1.08 KB
/
testGenrator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Created by chaclus on 2016/12/15.
*/
/**
* 生成器是一种可以从中退出之后重新进入的函数。生成器的环境(绑定的变量)会在每次执行后保存,下次进入时可以继续使用
*/
function* idMaker() {
var index = 0;
while(index < 3) {
yield index++;
}
}
var gen = idMaker();
console.log("gen....", gen);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
function* anotherGenerator(i) {
yield i + 1;
yield i + 2;
yield i + 3;
}
function* generator (i) {
yield i;
yield* anotherGenerator(i);
yield i + 10;
}
var gen = generator(10);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);