- 打印文字
打印效果如下:
class createLinkText {
constructor (name) {
console.log(name)
this.equeue = []
setTimeout(() => {
this.next()
}, 0)
}
register (fn, isF) {
if (isF) {
this.equeue.unshift(fn)
} else {
this.equeue.push(fn)
}
}
next () {
const fn = this.equeue.shift()
fn && fn()
}
eat (food) {
const _eat = () => {
console.log(food)
this.next()
}
this.register(_eat)
return this
}
sleep (time, isF) {
const _sleep = () => {
setTimeout(() => {
console.log(time)
this.next()
}, time * 1000);
}
this.register(_sleep, isF)
return this
}
sleepFirst (time) {
return this.sleep(time, true)
}
}
new createLinkText('张三').eat('早餐').sleep(2).sleepFirst(3).eat('晚餐')