实现LazyMan

题目:

实现一个LazyMan,可以按照以下方式调用:
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

以此类推。

 

 

方式一:

function _LazyMan(name) {

  // 任务队列
  this.taskList = [];

  var self = this;

  var fn = (function(name){
    return function() {
      console.log("Hi! This is " + name + "!");
      self.next();
    };
  })(name);

  this.taskList.push(fn);

  setTimeout(function(){
    self.next();    
  },0);           
}

_LazyMan.prototype.next = function() {
  if(this.taskList.length > 0) {
    var fn = this.taskList.shift();
    fn && fn();
  }
};

_LazyMan.prototype.eat = function(str) {
  var self = this;
  var fn = function() {
    console.log("Eat " + str + "~!");
    self.next();
  };
  this.taskList.push(fn);
  return this;
};

_LazyMan.prototype.sleep = function(time) {
  var self = this;
  var fn = function() {
    setTimeout(function(){
      console.log("Wake up after " + time + "!");
      self.next();
    }, time);
  };
  this.taskList.push(fn);
  return this;
};

_LazyMan.prototype.sleepAfter = function(time) {
  var self = this;
  var fn = function() {
    setTimeout(function(){
      console.log("Wake up after " + time + "!");
      self.next();
    }, time);
  };
  this.taskList.unshift(fn);
  return this;
};

function LazyMan(name) {
  return new _LazyMan(name);   
}

 

方式二:

(function(){

  var taskList = [];

  // 订阅
  function subscribe() {
    var param = {};
    var args = Array.prototype.slice.call(arguments);
  
    param.msg = args[0];
    param.args = args.slice(1);
   
    if(param.msg == "sleepFirst") {
      taskList.unshift(param);
    } else {
      taskList.push(param);
    }
  }

  // 发布
  function publish() {
    if(taskList.length > 0) {
      run(taskList.shift());
    }
  }

  // 运行
  function run(param) {
    var option = param.msg;
    var args = param.args;

    switch(option) {
      case "lazyMan": lazyMan.apply(null, args);break;
      case "eat": eat.apply(null, args);break;
      case "sleep": sleep.apply(null, args);break;
      case "sleepFirst": sleepFirst.apply(null, args);break;
      default:;
    }
  }

  // 各种方法
  function lazyMan(str) {
    LazyManLog("Hi! This is " + str);
    publish();
  }

  function eat(str) {
    LazyManLog("Eat " + str + "~");
    publish();
  }

  function sleep(time) {
    setTimeout(function(){
      LazyManLog("Wake up after " + str);
      publish();
    },time);
  }

  function sleepFirst(time) {
    setTimeout(function(){
      LazyManLog("Wake up after " + str);
      publish();
    },time);
  }

  // 输出
  function LazyManLog(str) {
    console.log(str);
  }

  // LazyMan类
  function LazyMan() {}

  LazyMan.prototype.eat = function(str) {
    subscribe("eat", str);
    return this;
  }

  LazyMan.prototype.sleep = function(time) {
    subscribe("sleep", time);
    return this;
  }

  LazyMan.prototype.sleepFirst = function(time) {
    subscribe("sleepFirst", time);
    return this;
  }

  window.LazyMan = function(name) {
    subscribe('lazyMan', str);
    setTimeout(function() {
      publish();
    }, 0);

    return new LazyMan();
  }

  
})();

 参考:

 http://div.io/topic/1915

 http://web.jobbole.com/89626/

 

posted @ 2017-02-09 15:52  dreamerjdw  阅读(222)  评论(0编辑  收藏  举报