CoffeeScript到Node.js语法问题“)”

时间:2011-08-29 09:59:00

标签: node.js coffeescript

我在第21行遇到“太多”问题“我无法将此咖啡脚本代码编译成Node.js 关于这些“)”的任何帮助? 感谢

amqp = require('amqp')

class Queue
    constructor: (ip = 'localhost') ->
            @ip = ip
            @receivedObject
            @connection = amqp.createConnection({ host: @ip })

    subscribeTaskQueue: (queueToSubscribe) ->
            self = @
            self.connection.on('ready', ->
                    q = self.connection.queue(queueToSubscribe)
                    q.bind('#')

                    q.subscribe({ ack: true }, (message) ->
                            self.receivedObject = message
                            console.log(self.receivedObject)
                    )
            )

    addTaskToQueue: (queue, objectToSend) ->
            @connection.publish(queue, objectToSend)

module.exports = Queue

1 个答案:

答案 0 :(得分:6)

这是CoffeeScript摇滚的一个方面:在使用参数调用函数时可以省略parens!这在Node中特别好用,其中回调通常是最后一个参数。

像这样:

amqp = require 'amqp'

class Queue

   constructor: (ip = 'localhost') ->
        @ip = ip
        @receivedObject
        @connection = amqp.createConnection { host: @ip } 

   subscribeTaskQueue: (queueToSubscribe) ->
        self = @
        self.connection.on 'ready', ->
                q = self.connection.queue queueToSubscribe
                q.bind '#'

                q.subscribe { ack: true }, (message) ->
                        self.receivedObject = message
                        console.log(self.receivedObject)

   addTaskToQueue: (queue, objectToSend) ->
        @connection.publish queue, objectToSend

module.exports = Queue