Javascript 在CoffeeScript中创建Singleton的首选方法

Javascript 在CoffeeScript中创建Singleton的首选方法,javascript,coffeescript,singleton,Javascript,Coffeescript,Singleton,我正在创建一个相对简单的“bag-o-functions” 在JS中,每当我想访问过于泛型而无法公开的本地助手方法时,我通常会执行以下操作: Util = new function () { var helper = function () {} this.myPublic = function () { // some code that uses the helper } } 在CoffeeScript中,这是一种可以接受的实现相同目标的方法吗 @U

我正在创建一个相对简单的“bag-o-functions”

在JS中,每当我想访问过于泛型而无法公开的本地助手方法时,我通常会执行以下操作:

Util = new function () {
    var helper = function () {}
    this.myPublic = function () {
        // some code that uses the helper
    }
}
在CoffeeScript中,这是一种可以接受的实现相同目标的方法吗

@Util = class
  helper = ->
  @myPublic = ->
    # some code that uses the helper

你可以这样做

Util = new (->
  helper = ->
  @myPublic = ->
    # some code that uses the helper
    return
  return
)

你可以这样做

Util = new (->
  helper = ->
  @myPublic = ->
    # some code that uses the helper
    return
  return
)