Created
July 3, 2012 09:17
-
-
Save jbgutierrez/3038690 to your computer and use it in GitHub Desktop.
Metaprogramación con Coffescript
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
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