Skip to content

Instantly share code, notes, and snippets.

@antw
Created October 11, 2011 20:55
Show Gist options
  • Save antw/1279408 to your computer and use it in GitHub Desktop.
Save antw/1279408 to your computer and use it in GitHub Desktop.
Class Inheritable Attributes in CoffeeScript
# 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