class LazyMan{
constructor(name){
this.name = name
this.taskList = []
setTimeout(()=>{
this.next()
})
}
eat(food){
const task=()=>{
console.log(`${this.name}吃${food}`)
this.next()
}
this.taskList.push(task)
return this
}
sleep(second){
const task = ()=>{
setTimeout(() => {
console.log(`${this.name}睡了${second}分钟`)
this.next()
}, second*5);
}
this.taskList.push(task)
return this
}
next(){
const task = this.taskList.shift()
if(task){
task()
}
}
}
const me = new LazyMan('喵喵')
me.eat('猫条').eat('猫罐头').sleep(5).eat('猫粮')
链式调用 lazyMan
最新推荐文章于 2025-06-11 11:02:32 发布