0

我知道如何用咖啡脚本编写回调函数,如下所示:

test1.coffee

exports.cube=(callback)-> 
    callback(5)

test2.coffee

test1=require('./test1')

test1.cube (result) ->
    console.log(result)

我想知道如何在回调函数中添加参数?这样我就可以像这样使用它:

test1.cube(para,result)->
    //use *para* to compute a *result*
    //here can do something with *result*
4

2 回答 2

1

如果我理解正确,你想要的是:

cube = (x, callback) ->
  callback(x * x * x)

cube 3, (result) ->
  console.log 'the cube of 3 is ', result
于 2012-09-10T09:18:34.053 回答
1

您可以使用内置方法 apply() 或 call() 之类的

callback.call(...)
callback.apply(...)

这里有更多关于它们之间的区别和区别: call 和 apply 有什么区别?

于 2012-09-10T12:00:58.190 回答