-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazyMan.js
More file actions
141 lines (132 loc) · 2.72 KB
/
lazyMan.js
File metadata and controls
141 lines (132 loc) · 2.72 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* LazyMan 的 ES5、ES6 实现
*
*
* LazyMan('daming')
* --> 'hi! daming'
*
*
* LazyMan('daming').sleep(10).eat('dinner')
* --> 'hi! daming'
* (等待10s)
* --> 10 seconds passed
* --> eat dinner
*
*
* LazyMan('daming').eat('apple').eat('banana')
* --> hi! daming
* --> eat apple
* --> eat banana
*
*
* LazyMan('daming').sleepFirst(10).eat('apple')
* (等待10s)
* --> 10 seconds passed
* --> hi! daming
* --> eat apple
*
*/
/**
* ES5 实现
*/
const LazyMan = function (name) {
if (!(this instanceof LazyMan)) return new LazyMan(name); // 可省去 new 实例
this.tasks = [];
this.tasks.push(() => {
console.log(`hi! ${name}`);
this.next();
})
setTimeout(() => {
this.next(); // 执行栈为空时才继续执行
}, 0);
}
LazyMan.prototype = {
next: function () {
if (this.tasks.length) {
const fn = this.tasks.shift();
fn & fn();
}
},
sleep: function (time) {
this.tasks.push(() => {
setTimeout(() => {
console.log(`${time} seconds passed`);
this.next();
}, time*1000);
});
return this;
},
sleepFirst: function (time) {
this.tasks.unshift(() => {
console.log(`please wait ${time} seconds`);
setTimeout(() => {
console.log(`${time} seconds passed`);
this.next();
}, time*1000);
});
return this;
},
eat: function (sth) {
this.tasks.push(() => {
console.log(`eat ${sth} now`);
this.next();
});
return this;
}
}
// LazyMan('daming');
// LazyMan('daming').eat('apple');
// LazyMan('daming').sleep(3).eat('banana');
// LazyMan('daming').eat('apple').sleep(3).eat('banana');
LazyMan('daming').sleepFirst(3).eat('apple');
/**
* ES6 实现
*/
class LazyMan6 {
constructor (name) {
this.tasks = [];
this.tasks.push(() => {
console.log(`hi! ${name}`);
this.next();
});
setTimeout(() => {
this.next();
}, 0);
}
next () {
if (this.tasks.length) {
const fn = this.tasks.shift();
fn & fn();
}
}
sleep (time) {
this.tasks.push(() => {
console.log(`please wait ${time} seconds`);
setTimeout(() => {
this.next();
}, time*1000);
});
return this;
}
sleepFirst (time) {
this.tasks.unshift(() => {
console.log(`please wait ${time} seconds`);
setTimeout(() => {
this.next();
}, time*1000);
});
return this;
}
eat (sth) {
this.tasks.push(() => {
console.log(`eat ${sth}`);
this.next();
});
return this;
}
}
const lm = (name) => new LazyMan6(name);
// lm('daming');
// lm('daming').eat('apple');
// lm('daming').sleep(3).eat('apple');
// lm('daming').eat('apple').sleepFirst(3).eat('banana');