Skip to content

Instantly share code, notes, and snippets.

@andreineculau
Created October 31, 2012 08:10
Show Gist options
  • Save andreineculau/3985768 to your computer and use it in GitHub Desktop.
Save andreineculau/3985768 to your computer and use it in GitHub Desktop.
call static properties of the current class in coffeescript
class A
@temp: 'A'
constructor: () ->
console.log A.temp
class B extends A
@temp: 'B'
constructor: () ->
super
console.log B.temp
b = new B()
# outputs 'A', 'B'
# intended output, but not really nice with all the named-references
class A
@temp: 'A'
constructor: () ->
console.log @constructor.temp
class B extends A
@temp: 'B'
constructor: () ->
super
console.log @constructor.temp
b = new B()
# outputs 'B', 'B'
# not the intended output
class A
@temp: 'A'
self = @ # @ = class, not this (the instance)
constructor: () ->
console.log self.temp
class B extends A
@temp: 'B'
self = @ # @ = class, not this (the instance)
constructor: () ->
super
console.log self.temp
b = new B()
# outputs 'A', 'B'
# intended output, pretty ok syntax
# from https://github.com/jashkenas/coffee-script/issues/1207#issuecomment-961677
@aral
Copy link

aral commented Apr 20, 2016

In case anyone stumbles upon this, #2 works in modern CoffeeScript (tested with v1.10.0) as it should. You should be using @constructor to get a reference to the ‘class’.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment