簡體   English   中英

在CoffeeScript中創建Singleton的首選方法

[英]Preferred Way of Creating Singleton in CoffeeScript

我正在創建一個相對簡單的“ 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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM