Skip to content

Instantly share code, notes, and snippets.

@changtimwu
Created October 8, 2012 02:22
Show Gist options
  • Save changtimwu/3850399 to your computer and use it in GitHub Desktop.
Save changtimwu/3850399 to your computer and use it in GitHub Desktop.
to demonstrate a typical singleton in coffeescript
class Parameter
_instance=null
someparam: 'bingo'
constructor: ()->
console.log 'Parameter got constructed'
showparam: ->
console.log 'someparam is ', @someparam
setparam: (s)->
@someparam = s
@get: ->
#_instance = new Parameter() if not _instance
_instance
_instance = new Parameter()
a = Parameter.get()
b = Parameter.get()
a.showparam()
b.showparam()
a.setparam 'qq'
a.showparam()
b.showparam()
a.someparam = 'ff'
b.showparam()
#can be required like the following
#param = require('parameter').get()
#param.mvr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment