咖啡脚本和节点中的静态方法

Static methods in coffeescript and node

本文关键字:静态方法 节点 脚本 咖啡      更新时间:2023-09-26

我对使用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) ->

将其附加到某处:

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

这将是"静态"方法。