[英]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.