Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Created July 3, 2012 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgutierrez/3038690 to your computer and use it in GitHub Desktop.
Save jbgutierrez/3038690 to your computer and use it in GitHub Desktop.
Metaprogramación con Coffescript
Function::hasProperties = (properties) ->
self = @
self.prototype.properties = {}
definePropertie = (key, value) ->
self.prototype.properties[key] = value
self.prototype[key] = (newValue) ->
@properties[key] = newValue
@
for key, value of properties
definePropertie key, value
class CoffeeCup
@hasProperties
strength: 'medium'
cream: false
sugar: false
toString: ->
"#{@properties.strength} - #{@properties.cream} - #{@properties.sugar}"
morningCup = new CoffeeCup()
morningCup.properties
eveningCup =
new CoffeeCup().
strength('dark').
cream(true).
sugar(true)
alert eveningCup.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment