SOURCE

function Man(name){
    this.name=name
    this.tasks=[]

    let fn = ()=>{
        console.log(`Hi I am ${this.name}`)
        this.next()
    }
    // this.tasks.push(fn)
    setTimeout(fn,0)
}

Man.prototype.eat = function(act){
    let fn = ()=>{
        console.log(`I am eating ${act}`)
        this.next()
    }
    this.tasks.push(fn)
    return this
}

Man.prototype.sleep = function(time){
    let fn = ()=>{
        setTimeout(()=>{
            console.log(`等待了${time}秒...`)
            this.next()
        },time*1000)
    }
    this.tasks.push(fn)
    return this
}

Man.prototype.sleepFirst = function(time){
    let fn = ()=>{
        setTimeout(()=>{
            console.log(`等待了${time}秒...`)
            this.next()
        },time*1000)
    }
    this.tasks.unshift(fn)
    return this
}

Man.prototype.next = function(){
    let fn = this.tasks.shift()
    fn && fn()
}

var LazyMan = function(name){
    return new Man(name)
}

LazyMan('Tony')
    .eat('lunch')
    .eat('dinner')
    .sleepFirst(5)
    .sleep(10)
    .eat('junk food')
console 命令行工具 X clear

                    
>
console