简体   繁体   English

coffeescript和node中的静态方法

[英]Static methods in coffeescript and node

I'm fairly new to using coffeescript and I have the following code 我对使用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

The problem I have is I want to call Aggregate.find 20 which in turn will return a new aggregate with that ID. 我遇到的问题是我想调用Aggregate.find 20,而Aggregate.find 20又返回一个带有该ID的新聚合。 Any advice on how to get this module to work like this would be greatly appreciated. 关于如何让这个模块像这样工作的任何建议将不胜感激。

Cheers Mike. 干杯迈克。

Your code should work fine, except that you have a syntax error in your constructor. 您的代码应该可以正常工作,除了您的构造函数中有语法错误。

Change: 更改:

constructor(id) ->

to: 至:

constructor: (id) ->

Append this somewhere: 在某处附加:

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

And it would be "static" method. 它将是“静态”方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM