非原创,面试挂了,整理一下答案
原文地址->掘金-子非-如何实现一个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
以此类推。
ES6 Class 实现
class _LazyMan {
constructor(name) {
this.tasks = []; // tasks维护将要执行的操作
const task = () => {
console.log(`Hi! This is ${name}!`);
this.next(); // 当前任务执行完毕后,继续执行下一个,后续task同理
};
this.tasks.push(task);
setTimeout(()=>{
this.next();
}, 0);
}
next() {
let task = this.tasks.shift();
task && task();
}
sleepFirst(time) { // 优先执行,需放置在队首
this.sleep(time, true);
return this;
}
sleep(time, first = false) {
let task = () => {
const DELAY = time * 1000;
setTimeout(()=>{
console.log(`Wake up after ${time}`);
this.next();
}, DELAY);
};
if(first) {
this.tasks.unshift(task);
} else {
this.tasks.push(task);
}
return this;
}
eat(food) {
let task = () => {
console.log(`Eat ${food}~`);
this.next();
};
this.tasks.push(task);
return this;
}
}
const LazyMan = (name) => {
return new _LazyMan(name);
}
LazyMan('Hank').sleep(10).eat('dinner').sleepFirst(3)
原型链继承,借用箭头函数实现版
function _LazyMan(name) {
this.tasks = [];
let task = () => {
console.log(`Hi! This is ${name}!`);
this.next();
};
this.tasks.push(task);
setTimeout(() => {
this.next();
}, 0);
return this;
}
_LazyMan.prototype.next = function () {
const task = this.tasks.shift();
task && task();
};
_LazyMan.prototype.sleep = function (time, isFirst = false) {
const DELAY = time * 1000;
let task = () => {
setTimeout(() => {
console.log(`Wark up after ${time}`);
this.next();
}, DELAY);
};
isFirst ? this.tasks.unshift(task) : this.tasks.push(task);
return this;
};
_LazyMan.prototype.sleepFirst = function (time) {
this.sleep(time, true);
return this;
};
_LazyMan.prototype.eat = function (food) {
let task = () => {
console.log(`Eat ${food}~`);
this.next();
};
this.tasks.push(task);
return this;
};
function LazyMan(name) {
return new _LazyMan(name);
}
LazyMan('Hank').eat('dinner').sleep(3).sleepFirst(5);