我试图在我的腰带上提供一些例子,说明你如何在CoffeeScript中使用不同的方法来实现JavaScript.在这个排队函数的例子中,我对如何在CoffeeScript中处理它感到困惑
wrapFunction = (fn, context, params) ->
return ->
fn.apply(context, params)
sayStuff = (str) ->
alert(str)
fun1 = wrapFunction(sayStuff, this, ['Hello Fun1'])
fun2 = wrapFunction(sayStuff, this, ['Hello Fun2'])
funqueue = []
funqueue.push(fun1)
funqueue.push(fun2)
while (funqueue.length > 0) {
(funqueue.shift())();
}
特别是我如何在CoffeeScript中重写它?
while (Array.length > 0) {
(Array.shift())();
解决方法:
f1 = (completeCallback) ->
console.log('Waiting...')
completeCallback()
funcs = [ f1, f2, f3 ]
next = ->
if funcs.length > 0
k = funcs.shift()
k(next)
next()