class _LazyMan {
constructor(name){
let that = this
this.tasks = [],
this.fn = ((n)=>{
return ()=>{
console.log("this is "+ n +"!(By Class)")
that.next()
}
})(name)
this.tasks.push(this.fn)
setTimeout(() => {
that.next()
}, 0);
return this
}
next(){
var fn = this.tasks.shift()
fn && fn()
}
eat(food){
let fn = ((f)=>{
return ()=>{
console.log('eat '+ f)
this.next()
}
})(food)
this.tasks.push(fn);
return this
}
sleep(delay,immedia = false){
let that = this
let fn = ((d,i)=>{
return ()=>{
setTimeout(()=>{
console.log(`${i ? 'sleepFisrt ' : 'sleep '}` + (d/1000) + 's')
that.next()
},d)
}
})(delay,immedia)
immedia ? this.tasks.unshift(fn) : this.tasks.push(fn);
return this
}
sleepFirst(delay){
return this.sleep(delay,true)
}
}
let LazyMan = function (name){
return new _LazyMan(name)
}
LazyMan('').eat('fish').sleepFirst(1000).sleep(2000)
console