-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.html
More file actions
67 lines (61 loc) · 1.64 KB
/
Generator.html
File metadata and controls
67 lines (61 loc) · 1.64 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/*执行 Generator 函数会返回一个遍历器对象,
也就是说,Generator 函数除了状态机,还是一个遍历器
对象生成函数。返回的遍历器对象,可以依次遍历 Generat
or 函数内部的每一个状态。
*/
var x = 2;
function* learnGenerator() {
// console.log(1);
var z = (yield x)*3;
yield z;
// console.log(3);
return "ending"
}
/*调用 Generator 函数后,该函数并不执行,返
回的也不是函数运行结果,而是一个指向内部状态的指针对
象,也就是上一章介绍的遍历器对象(Iterator Object)。
*/
var generator = learnGenerator();
console.log(generator.next());
console.log(generator.next(20));
generator.next();
let go = function*(x) {
console.log('x'+":"+x)
let a = yield x
console.log('xx', x)
console.log('a', a)
let b = yield x + 1
sum = a + b
yield a + b
return a + b
}
var Go = go(5);
console.log(Go.next());
console.log(Go.next(10));//5,undefined
console.log(Go.next(10).value);
console.log(Go.next());
// 用到yield*表达式,用来在一个 Generator 函数里面执行另一个 Generator 函数。
function* foo () {
yield "a";
yield "b";
}
function* bar () {
yield "c";
yield* foo();
yield "d";
}
var Bar = bar();
for (let obj of Bar){
console.log(obj);
}
</script>
</body>
</html>