Skip to content

Instantly share code, notes, and snippets.

@cjbottaro
Created September 8, 2011 23:20
Show Gist options
  • Save cjbottaro/1205075 to your computer and use it in GitHub Desktop.
Save cjbottaro/1205075 to your computer and use it in GitHub Desktop.
class vs instance vars in CoffeeScript
class Blah
@counter = 0
@incr: ->
return @counter += 1
constructor:
@counter = 0
incr: ->
@counter += 1
b1 = new Blah
b2 = new Blah
b1.incr()
console.log(Blah.counter)
console.log(b1.counter)
console.log(b2.counter)
b2.incr()
console.log(Blah.counter)
console.log(b1.counter)
console.log(b2.counter)
Blah.incr()
console.log(Blah.counter)
console.log(b1.counter)
console.log(b2.counter)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment