必赢娱乐

必赢娱乐棋牌:Keep the fifth star for its lack of intelligence

16 6月 , 2019  

© 本文版权归作者  胭脂虫
 所有,任何形式转载请联系作者。

如何实现一个 LazyMan?

2016/12/24 · JavaScript
· 9 评论 ·
Javascript,
异步

本文作者: 伯乐在线
Natumsol
。未经作者许可,禁止转载!
欢迎加入伯乐在线 专栏作者

3月份找实习的时候,微信面试官给了我一套笔试题,今天整理时无意中翻了出来,其中有一道题特别有意思:

实现一个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

以此类推。

这是典型的JavaScript流程控制,问题的关键是如何实现任务的顺序执行。在Express有一个类似的东西叫中间件,这个中间件和我们这里的吃饭、睡觉等任务很类似,每一个中间件执行完成后会调用next()函数,这个函数用来调用下一个中间件。

对于这个问题,我们也可以利用相似的思路来解决,首先创建一个任务队列,然后利用next()函数来控制任务的顺序执行:

JavaScript

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); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);
}

打赏支持我写出更多好文章,谢谢!


打赏作者

When she sit on the bench , she saw an old lady staring at the sea. She
realized that it’s time to stop their sinking.

关于作者:Natumsol

必赢娱乐棋牌 1

阿里巴巴 前端工程师
个人主页
·
我的文章
·
5
·
   

必赢娱乐棋牌 2

Civilization domesticates you to rule. It shapes your behavior for
avoiding the challenge of understanding and instruction.

打赏支持我写出更多好文章,谢谢!

任选一种支付方式

必赢娱乐棋牌 3
必赢娱乐棋牌 4

2 赞 11 收藏 9
评论


相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图