js流程控制——Lazyman的两种实现方式

实现一个LazyMan, 可以按下列方式调用:

LazyMan("Hank")输出:
Hi! This is Hank!

LazyMan("Hank").sleep(10).eat("dinner") 输出:
Hi! This is Hank!
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") 输出:
Wake up after 5
Hi This is Hank!
Eat supper

方式一:

function _LazyMan(name) {
    this.taskList = []; 
    this.introduce(name);
};
 _LazyMan.prototype = {
    _bind: function(method, fn) {
        if(method === 'sleepFirst') {
            this.taskList.unshift(fn);
        }else {
            this.taskList.push(fn);
        }
    },
    next: function() {
        if(this.taskList.length > 0) {
            var fn = this.taskList.shift();
            fn && fn();
        }
    },
    introduce: function(name) {
        var me = this;
        // 在下一个事件循环启动任务
        this._bind('introduce', function() {    
            me.log('Hi! This is '+ name +'!');             
            me.next(); 
        });  

        setTimeout(function() {
            me.next();
        }, 0);
        return this;
    },
    sleep: function(sec) {
        var me = this;
        this._bind('sleep', function() {
            me.log('Wake up after ' + sec);
            setTimeout(function() {
                me.next();
            }, sec * 1000);
        });
        return this;
    },
    eat: function(str) {
        var me = this;
        this._bind('eat', function() {
            me.log('Eat '+ str +'~');
            me.next(); 
        });
        return this; 
    },
    sleepFirst: function (sec) {
        var me = this;
        this._bind('sleepFirst', function() {
            me.log('Wake up after ' + sec);
            setTimeout(function() {
                me.next();
            }, sec * 1000); 
        });
        return this;
    },
    log: function(str) {
        console.log(str);
    }
}; 

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

 // LazyMan("Hank");
// LazyMan("Hank").sleep(10).eat("dinner");
// LazyMan("Hank").eat("dinner").eat("supper");
LazyMan("Hank").sleepFirst(5).eat("supper");

方式二:

class _LazyMan {
    constructor(name) {
        this.taskList = [];
        this.introduce(name);
    }
    _bind(method, fn) {
        if(method === 'sleepFirst') {
            this.taskList.unshift(fn);
        }else {
            this.taskList.push(fn);
        }
    }
    introduce(name) {
        const me = this;
        this._bind('introduce', function() {
            return new Promise(function(resolve, reject) {
                me.log('Hi! This is '+ name +'!');
                resolve();
            });
        });
        let sequence = Promise.resolve();
        // 在下一个事件循环启动任务
        setTimeout(function() {
            for (let i = 0; i < me.taskList.length; i++) {
                const func = (function (fn) {
                    return fn;
                })(me.taskList[i]);
                sequence = sequence.then(func);
            };
        }, 0);
        return this;
    }
    sleep(sec) {
        const me = this;
        this._bind('sleep', function() {
            return new Promise(function(resolve, reject) {
                me.log('Wake up after ' + sec);
                setTimeout(function() {
                    resolve();
                }, sec * 1000);
             });
        });
        return this;
    }
    eat(str) {
        const me = this;
        this._bind('eat', function() {
            return new Promise(function(resolve, reject) {
                me.log('Eat '+ str +'~');
                resolve();
            });
        });
        return this;
    }
    sleepFirst(sec) {
        const me = this;
        this._bind('sleepFirst', function() {
            return new Promise(function(resolve, reject) {
                me.log('Wake up after ' + sec);
                setTimeout(function() {
                    resolve();
                }, sec * 1000);
            });
         });
        return this;
    }
    log(str) {
        console.log(str);
    }
};

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

// LazyMan("Hank");
// LazyMan("Hank").sleep(10).eat("dinner");
// LazyMan("Hank").eat("dinner").eat("supper");
LazyMan("Hank").sleepFirst(5).eat("supper");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值