SOURCE

console 命令行工具 X clear

                    
>
console
function LazyMan(name) {
  this.tasks = [];
  let self = this;
  var fn = function() {
    console.log(`Hi, this is ${name}`);
    self.next();
  };
  this.tasks.push(fn);
  setTimeout(function() {
    self.next();
  }, 0);
}
LazyMan.prototype.next = function() {
  if (this.tasks.length) {
    let fn = this.tasks.shift();
    fn();
  }
}
LazyMan.prototype.sleep = function(num) {
  let self = this;
  var fn = function() {
    setTimeout(function() {
      console.log(`wake up after ${num}`);
      self.next();
    }, num*1000);
  }
  self.tasks.push(fn);
  return self;
}
LazyMan.prototype.eat = function(some) {
  let self = this;
  var fn = function() {
    console.log(`eat ${some}`);
    self.next();
  }
  self.tasks.push(fn);
  return self;
}
LazyMan.prototype.sleepFirst = function(num) {
  let self = this;
  var fn = function() {
    setTimeout(function() {
      console.log(`wake up after ${num}`);
      self.next();
    }, num*1000);
  }
  self.tasks.unshift(fn);
  return self;
}
var lazyman = function(name) {
  return new LazyMan(name);
}

lazyman('Matthew').sleep(3).eat('dinner').sleepFirst(5);
<pre>
LazyMan(“Hank”)输出:
Hi! This is Hank!

LazyMan(“Hank”).sleep(10).eat(“dinner”)输出
Hi! This is Hank!
//等待10秒..
Wake up after 10
Eat dinner~

LazyMan(“Hank”).eat(“dinner”).eat(“supper”)输出
Hi This is Hank!
Eat dinner~
Eat supper~

LazyMan("Hank").sleepFirst(5).eat("supper")输出
//等待5秒
Wake up after 5
Hi This is Hank!
Eat supper
</pre>