javascript - CoffeeScript 和 Node 中的静态方法

标签 javascript node.js coffeescript

我对使用 CoffeeScript 相当陌生,我有以下代码

eventBus = require './eventBus'                      

class Aggregate                                       
  constructor(id) ->                                  
    @_id = id                                         

  @find: (id) ->                                                              
    console.log "Find Called"
    new Aggregate(id)                                                 

  @apply: (event) ->                                                          
    @_total += event.attribute.amount                                         

  @emit: (event) ->                                                           
     EventBus.store event                                                     

module.Aggregate = Aggregate

我遇到的问题是我想调用 Aggregate.find 20 这反过来将返回具有该 ID 的新聚合。任何关于如何让这个模块像这样工作的建议将不胜感激。

干杯迈克。

最佳答案

您的代码应该可以正常工作,只是构造函数中有语法错误。

更改:

constructor(id) ->

至:

constructor: (id) ->

关于javascript - CoffeeScript 和 Node 中的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10460354/

相关文章:

javascript - 搜索和滚动条下拉子菜单 Bootstrap

javascript - 为无序项目分配编号

javascript - 使用 bodyParser 和 Router() 无法检索表单输入

ruby - 在 Rails3.1 ubuntu 11.04 中使用 CoffeeScript

javascript - 未找到 meteor 调用方法

coffeescript - for循环中的索引变量(_i)?

javascript - 什么情况下可以卸载根组件?

javascript - 如何连接到 mongodb 以及如何使用 node.js 程序从 mongodb 数据库打印所有集合的列表

javascript - 使用 'readline' 同步执行 node.js 程序

javascript - 这是什么错误 ERROR Cannot prepare tests due to an error.类型错误 : Cannot read property 'Date' of undefined?