实现:
// Person('Li')
/* 输入:
Hi! This is Hank!
*/
// Person('Dan').sleep(3).eat('dinner')
/* 输入:
Hi! This is Hank!
// 期待 10 秒..
Wake up after 10
Eat dinner~
*/
// Person('Jerry').eat('dinner~').eat('supper~')
/* 输入:
Hi This is Hank!
Eat dinner~
Eat supper~
*/
Person('Smith').sleepFirst(2).eat('supper')
/* 期待 5 秒,输入
Wake up after 5
Hi This is Hank!
Eat supper
*/
function Person(name) {return new CreatePerson(name)
}
class CreatePerson {constructor(name){this.tasks = [] // 外围,应用队列(数组)来存储要执行的函数
this.tasks.push(()=> console.log(`Hi! This is ${name}`))
this.runTask()}
runTask() {
// 要害!利用宏工作将事件提早执行
// 如果遇到 sleepFirst,能够先将 sleepFirst 压入工作队列,而后按程序执行
setTimeout(() => this.exector())
}
exector() {if(this.tasks.length === 0) return // 递归进口
const task = this.tasks.shift() // 取出工作
if(typeof task === "number") {setTimeout(() => {console.log(`Wake up after ${task}`)
this.exector() // 递归本身}, 2000)
}
if (typeof task === 'function') {task()
this.exector() // 递归本身}
return this
}
// 保护各种工作,用工作队列存储
eat(food) {this.tasks.push(() => console.log(`Eat ${food}~`)) // 惯例操作,塞进工作队列
return this
}
sleep(time) {this.tasks.push(time) // 惯例操作,塞进工作队列
return this
}
sleepFirst(time) {console.log(1);
this.tasks.unshift(time) // 执行:塞进第一项!return this
}
}