-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lazyman.js
65 lines (61 loc) · 1.37 KB
/
Lazyman.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @Author: pacino
* @Date: 2017-02-07 01:53:58
# @Email: [email protected]
* @Last Modified by: pacino
* @Last Modified time: 2017-02-07 02:16:45
*/
'use strict';
//一道微信的面试题
function _Lazyman() {
this.task = [];
var self = this;
var fn = function(name) {
console.log("Hi" + name);
self.next();
};
this.task.push(fn);
setTimeout(function() {
self.next();
}, 0);
};
_Lazyman.prototype = {
next: function() {
var fn = this.task.shift();
fn && fn();
},
sleep: function(time) {
var self = this;
var fn = function() {
setTimeout(function() {
console.log("go sleep");
}, time);
self.next();
};
this.task.push(fn);
return this;
},
sleepfirst: function(time) {
var self = this;
var fn = function() {
setTimeout(function() {
console.log("go sleep first");
}, time);
self.next();
}
this.task.unshift(fn);
return this;
},
eat: function(food) {
var self = this;
var fn = function() {
console.log("go eat" + food);
self.next();
};
return this;
}
};
function Lazyman(name) {
return new _Lazyman();
};
Lazyman("jack").eat("apple").sleepfirst(1000);