let index = 0
let stack = []
function next(){
let fn = stack[index]
index++
if(typeof fn === 'function') {
fn()
}
}
function T(name) {
stack.push(function() {
console.log('Hi! This is' + name)
next()
})
}
function task(name) {
return new T(name)
}
T.prototype.sleep = function(delay) {
stack.push(function() {
setTimeout(() => {
console.log('sleep have run end')
next()
},delay)
})
return this
}
T.prototype.eat = function() {
stack.push(function() {
console.log('eat have run end')
next()
})
return this
}
task('zhangsan').sleep(1000).eat().sleep(1000).eat()
next()
JS 实现函数链式调用
最新推荐文章于 2024-03-14 15:25:08 发布