3

我正在尝试获取一些示例,说明您将如何在 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())(); 
4

2 回答 2

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

funqueue = [fun1, fun2]

el() for el in funqueue
于 2010-11-19T17:17:05.803 回答
3
f1 = (completeCallback) ->
  console.log('Waiting...')
  completeCallback()

funcs = [ f1, f2, f3 ]

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

next()
于 2010-11-19T17:51:54.550 回答