SOURCE

function LazyMan(name) {
  return new LazyManObj(name)
}

function LazyManObj(name) {
  this.name = name
  this.queue = []
  
  setTimeout(() => {
    this.next()
  }, 0)
}

LazyManObj.prototype.next = function () {
  let task = this.queue.unshift()
  con
  task && task()
}

LazyManObj.prototype.sayHi = function () {
  console.log(`This is ${this.name}!`)
  this.next()
}

LazyManObj.prototype.sleep = function (time) {
  this.queue.push(() => {
    setTimeout(() => {
      console.log(`Wake up after ${time}`)
      this.next()
    }, time * 1000)
  })
  return this
}

LazyManObj.prototype.eat = function (something) {
	this.queue.push(() => {
    console.log(`Eat ${something}`)
    this.next()
  })
  return this
}

LazyManObj.prototype.sleepFirst = function(time) {
  this.queue.unshift(() => {
    setTimeout(() => {
      console.log(`Wake up after ${time}`)
      this.next()
    }, time * 1000)
  })
  return this
}

LazyMan("Hank").eat("dinner").sleep(2).eat("supper")




console 命令行工具 X clear

                    
>
console