Coffeescript,我将如何编写这个排队的函数示例,尤其是循环?

时间:2010-11-19 16:54:25

标签: javascript coffeescript

我正试图在我的腰带上提供一些例子,说明你如何在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())(); 

2 个答案:

答案 0 :(得分:4)

fun1 = -> alert 'Hello Fun1'
fun2 = -> alert 'Hello Fun2'

funqueue = [fun1, fun2]

el() for el in funqueue

答案 1 :(得分:3)

f1 = (completeCallback) ->
  console.log('Waiting...')
  completeCallback()

funcs = [ f1, f2, f3 ]

next = ->
  if funcs.length > 0
    k = funcs.shift()
    k(next)

next()