-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyman.js
107 lines (91 loc) · 2.09 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
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
// function lazyman(name){
// console.log(name);
// var queue = []
// return {
// sleep:function(time){
// setTimeout(function(){
// queue.map(function(ele){
// ele()
// })
// },time*1000)
// return this;
// },
// eat:function(food){
// queue.push(function(){
// console.log("Eat " + food)
// })
// return this;
// }
// }
// }
// lazyman("fry").sleep(10).eat("dinner")
// var foo = {
// a:"foo"
// }
// var bar = {
// a:"bar",
// show:function(){
// setTimeout(()=>{
// console.log (this.a)
// })
// }
// }
// var a = 'foo'
// var bar = {
// a:"bar",
// show:function(){
// setTimeout(function(){
// console.log (this.a)
// })
// }
// }
// bar.show()
function lazyman(name){
var tasks = [];
tasks.push(function(){
console.log("This is " + name)
next();
})
function next(){
var task = tasks.shift();
task && task()
}
setTimeout(next,0)
return {
sleep:function(n){
tasks.push(function(){setTimeout(next,n*1000)})
return this;
},
eat: function(food){
tasks.push(function(){
console.log("eat " + food)
next()
})
return this;
},
sleepfirst:function(n){
tasks.unshift(function(){setTimeout(next,n*1000)})
return this;
}
}
}
lazyman("fry").sleep(1).sleep(3).eat('dinner')
// lazyman('fry').eat('dinner').sleepfirst(5)
// lazyman('fry').eat('dinner').sleepfirst(5).eat('supper').sleep(5)
// var goon = (function(){
// var temp = lazyman("fry")
// return function(param){
// if(typeof param === "string"){
// temp.eat(param)
// }else{
// temp.sleep(param)
// }
// }
// })();
// goon(3)
// goon('rice')
// var a = {
// fn:(function(){
// console.log("oops")
// })()
// }