Skip to content

Instantly share code, notes, and snippets.

@grignaak
Created February 20, 2011 06:15
Show Gist options
  • Save grignaak/835766 to your computer and use it in GitHub Desktop.
Save grignaak/835766 to your computer and use it in GitHub Desktop.
Meta-programming in coffescript
class Entity
applyEvent: (event, args) ->
@[Entity._handler event] args...
@_handler: (name) -> "on_#{name}"
@event: (name, handler) ->
@::[name] = ->
@applyEvent name, arguments
@::[Entity._handler name] = handler
# --------
class Foo extends Entity
constructor: (@me = 10) ->
@event 'hello', (subject) ->
alert "hello, #{subject} #{@me}"
f = new Foo
f.hello 'world'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment