class LazyMan {
constructor(attr){
console.log(`I am ${attr}`)
this.cb = [] // 用于存放回调函数
setTimeout(() => {
this.runCb()
},0)
}
sleep(num){
let that = this
let fn = (function(n){
return function(){
setTimeout(() => {
console.log(`等了${n}秒`)
that.runCb()
},n)
}
})(num)
this.cb.push(fn)
return this
}
sleepFirst(num){
let that = this
let fn = (function(n){
return function(){
setTimeout(() => {
console.log(`wating ${n} 秒`)
that.runCb()
},n)
}
})(num)
this.cb.unshift(fn)
return this
}
runCb(){
let fn = this.cb.shift()
fn && fn()
}
eat(attr){
let that = this
let fn = (function(str){
return function(){
console.log(`I am eating ${str}`)
that.runCb()
}
})(attr)
this.cb.push(fn)
return this
}
}
new LazyMan('Tom').eat('brunch').eat('tea').sleepFirst(2000)
console