SOURCE

// 先把任务添加完成,再一次执行任务(通过任务循环)

function LazyMan( name ){
    this.name = name;
    this.tasks = [['introduce']]
    this.sleepfirst = function (sec){
        this.tasks.shift();
        this.tasks.push(['sleep', sec]);
        this.tasks.push(['introduce']);
        return this
    }
    this.sleep = function (sec){
        this.tasks.push(['sleep', sec]);
        return this
    }
    this.eat = function (t){
        this.tasks.push(['eat',t]);
        return this
    }
    this.do = function(){
        if (this.tasks.length === 0) {
                return;
            }
            const task = this.tasks.shift();
            if (task[0] === 'introduce') {
                console.log(`Hi! This is ${this.name}`);
                this.do();
            } else if (task[0] === 'sleep') {
                setTimeout(() => {
                    console.log(`Wake up after ${task[1]}`);
                    this.do();
                }, task[1] * 1000);
            } else if (task[0] === 'eat') {
                console.log(`Eat ${task[1]}~`);
                this.do();
            }
    }
    setTimeout(() => {
        this.do()
    })

    return this
}

LazyMan('11').sleep(1).eat(11)

console 命令行工具 X clear

                    
>
console