Created
October 31, 2012 08:10
-
-
Save andreineculau/3985768 to your computer and use it in GitHub Desktop.
call static properties of the current class in coffeescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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’.