Created
October 11, 2011 20:55
-
-
Save antw/1279408 to your computer and use it in GitHub Desktop.
Class Inheritable Attributes 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
# So messy. There must be a better way... | |
class Model | |
@property: (name) -> | |
if @properties? and @properties.length is @.__super__?.constructor.properties?.length | |
@properties = Array::slice.call @.__super__.constructor.properties, 0 | |
else if not @properties? | |
@properties = [] | |
@properties.push name | |
props: -> | |
@constructor.properties or [] | |
class Something extends Model | |
@property 'something' | |
class SomethingElse extends Something | |
@property 'another' | |
class Separate extends Model | |
@property 'final' | |
console.log 'Model', (new Model).props() | |
console.log 'Something', (new Something).props() | |
console.log 'SomethingElse', (new SomethingElse).props() | |
console.log 'Separate', (new Separate).props() | |
# => Model [] | |
# => Something ["something"] | |
# => SomethingElse ["something", "another"] | |
# => Separate ["final"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment