-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.html
81 lines (62 loc) · 1.32 KB
/
Generator.html
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<script type="text/javascript">
/*
Generator:
当调用Generator函数的时候,函数内的代码不会执行
*/
/*
使用yield会继续等待next调用
使用return就会终止整个函数。内部如果有多个return只会执行第一个return后的值,后面如果还调用next,那么会为undefined。
*/
// function* fn(){
// //alert(1);
// yield 'hello';
// yield '世界';
// return '呵呵哒';
// }
// var hw = fn(); //并不弹1
//
// console.log(hw.next().value); //hello
// console.log(hw.next().value);//世界
// console.log(hw.next().value);//呵呵哒
// console.log(hw.next().value);//undefined
/*
如何使用Generator将下列数组中的数字取出来
*/
var arr = [1,[2,3,[4,55],6]];
function *fn(arr){
for(var i=0,len = arr.length;i<len;i++){
if(typeof arr[i] !== 'number'){
yield*fn(arr[i]);
}else{
yield arr[i];
}
}
}
for(var i of fn(arr)){
//console.log(i);
}
/*
参数设值
*/
function *fn1(x){
var a = 2 * (yield x+5);
var b = a + (yield a);
return (a+b+5);
}
//6
//a = 16+(16+4)+5
//3
var d = fn1(1);
console.log(d.next());
console.log(d.next(8));
console.log(d.next(4));
</script>
</body>
</html>