Skip to content

Instantly share code, notes, and snippets.

@benshimmin
Created March 25, 2012 14:01
Show Gist options
  • Save benshimmin/2194809 to your computer and use it in GitHub Desktop.
Save benshimmin/2194809 to your computer and use it in GitHub Desktop.
Singletons in CoffeeScript
# CoffeeScript makes this almost uniquely easy.
class Singleton
instance = undefined
@getInstance : ->
instance ?= new @
# And a singleton class can be subclassed elegantly too.
class SingletonTest extends Singleton
constructor : ->
@property = 100
t1 = SingletonTest.getInstance()
console.log t1.property # -> 100
t1.property = 200
t2 = SingletonTest.getInstance()
console.log t2.property # -> 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment