lazyman 函数-链式

要求:1、链式操作

   2、叫什么名字   LazyMan

   3、第一个执行的函数  firstFn

   4、等多少秒后做什么   sleep

   5、做什么运动  sport

 

function LazyMan(name) {

    var self = this;
    this.tasks = [];
    var fun = (function (name) {
        var _name = name;
        return function () {
            console.log("I,m name " + _name);
            self.next();
        }
    })(name)
   this.tasks.push(fun);

   setTimeout(function(){
    var fun = self.tasks.shift();
    fun && fun();
   },0)
    return this;
}

LazyMan.prototype.next = function(){
    var fun = this.tasks.shift();
    fun && fun();
}
LazyMan.prototype.sleep = function(time){

    var self = this;
    var fun = (function (name) {
        var _name = name;
        return function () {
          setTimeout(function(){
            console.log("wake up after  " + _name);
            self.next();
          },_name*1000)
        }
    })(time)
    this.tasks.push(fun);

    return this;
}
LazyMan.prototype.sport = function(thing){
  
    var self = this;
    var fun = (function (name) {
        var _name = name;
        return function () {
            console.log("can some thing " + _name);
            self.next();
        }
    })(thing)
    this.tasks.push(fun);

    return this;
}

LazyMan.prototype.firstFun = function(time){

    var self = this;
    var fun = (function (name) {
        var _name = name;
        return function () {
            setTimeout(function(){
                console.log("wake up after  " + _name);
                self.next();
              },_name*1000)
        }
    })(time)
    this.tasks.unshift(fun);

    return this;
}
function lazy(name){
    return new LazyMan(name)
}

lazy("fasdfa").sleep(2).sport('ping ping').firstFun(4)

//wake up after  4
//I,m name fasdfa
//wake up after  2
//can some thing ping ping

 

posted @ 2018-06-04 16:48  ypm_wbg  阅读(127)  评论(0)    收藏  举报