zoukankan      html  css  js  c++  java
  • 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.tasks = [];   
        var self = this;
        var fn =(function(n){
            var name = n;
            return function(){
                console.log("Hi! This is " + name + "!");
                self.next();
            }
        })(name);
        this.tasks.push(fn);
        setTimeout(function(){
            self.next();
        }, 0); // 在下一个事件循环启动任务
    }
    /* 事件调度函数 */
    _LazyMan.prototype.next = function() { 
        var fn = this.tasks.shift();
        fn && fn();
    }
    _LazyMan.prototype.eat = function(name) {
        var self = this;
        var fn =(function(name){
            return function(){
                console.log("Eat " + name + "~");
                self.next()
            }
        })(name);
        this.tasks.push(fn);
        return this; // 实现链式调用
    }
    _LazyMan.prototype.sleep = function(time) {
        var self = this;
        var fn = (function(time){
            return function() {
                setTimeout(function(){
                    console.log("Wake up after " + time + "s!");
                    self.next();
                }, time * 1000);
            }
        })(time);
        this.tasks.push(fn);
       return this;
    }
    _LazyMan.prototype.sleepFirst = function(time) {
        var self = this;
        var fn = (function(time) {
            return function() {
                setTimeout(function() {
                    console.log("Wake up after " + time + "s!");
                    self.next();
                }, time * 1000);
            }
        })(time);
        this.tasks.unshift(fn);
        return this;
    }
    /* 封装 */
    function LazyMan(name){
        return new _LazyMan(name);
    }
    

      

    举栗子:

    LazyMan('Hank').sleepFirst(5).eat('supper')
    // _LazyMan {tasks: Array(3)}
    // Wake up after 5s!
    // Hi! This is Hank!
    // Eat supper~
    

      

    LazyMan('Hank').eat('dinner').eat('supper')
    
    //_LazyMan {tasks: Array(3)}
    // Hi! This is Hank!
    // Eat dinner~
    // Eat supper~
    

      

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

  • 相关阅读:
    通向KDE4之路(七):文档反省器Okular和Ligature
    KDE言语绑定──KDEBindings
    KDEEdu(教诲性质软件)引见
    KDEMultimedia(KDE多媒体东西)引见
    KDESDK(KDE斥地工具)引见
    通向KDE4之路(十五):Konsole年夜整修
    企业信息化规划http://www.blogcn.com/User/fieldnet/index.html
    写在博客一周年
    Delphi 的内存操作函数(3): 给结构体指针分配内存
    在 Delphi 2009 中, for in 循环都能用在什么地方?
  • 原文地址:https://www.cnblogs.com/lulin1/p/9209328.html
Copyright © 2011-2022 走看看