zoukankan      html  css  js  c++  java
  • 实现lazyMan

    class LazyMan {
        constructor(name){
             this.nama = name;
            this.queue = [];
            this.queue.push(() => {
                console.log("Hi! This is " + name + "!");
                this.next();
            })
            setTimeout(()=>{
                this.next()
            },0)
        }
    
        eat(name){
            this.queue.push(() =>{
                console.log("Eat " + name + "~");
                this.next()
            })
            return this;
          }
          
          next(){
            var fn = this.queue.shift();
            fn && fn();
          }
    
          sleep(time){
            this.queue.push(() =>{
                setTimeout(() => {
                    console.log("Wake up after " + time + "s!");
                    this.next()
                },time * 1000)
            })
            return this;
        }
    
        sleepFirst(time){
            this.queue.unshift(() =>{
                setTimeout(() => {
                    console.log("Wake up after " + time + "s!");
                    this.next()
                },time * 1000)
            })
            return this;
        }
    
    }
    
    function creatLazyMan(name){
        return new LazyMan(name)
    }
    creatLazyMan("Hank").sleep(10).eat("dinner")
    // Hi! This is Hank!
    // Wake up after 10s!
    // Eat dinner~
  • 相关阅读:
    正则表达式
    爬虫原理和网页构造
    简单的博客系统之二
    配置编辑器geany
    linux删除多文件
    eNSP交换路由基础
    NTP centOS6.5
    shell脚本之lftp上传
    进度条
    maketrans与translate函数
  • 原文地址:https://www.cnblogs.com/AwenJS/p/12713115.html
Copyright © 2011-2022 走看看