我试图让我的你将如何在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()

其他提示

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

funqueue = [fun1, fun2]

el() for el in funqueue
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top