-
-
Save wesvetter/2cd2501e704d96c047cb9198048b8fa4 to your computer and use it in GitHub Desktop.
A small collection of generic CoffeeScript helper functions.
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
# shorthand wrapper for `console.log` | |
put = (args...) -> console.log args... | |
# initialise undefined vars: `[a, b, c] = init 3` | |
init = (amount) -> undefined for n in [1..amount] | |
# decorator for defining constructors (see example.coffee) | |
factory = (mutator) -> (args...) -> | |
mutator (self = Object.create null), args... | |
return self | |
# decorator for defining blessings (see example.coffee) | |
bless = (mutator) -> (self, args...) -> | |
mutator self, args... | |
return self |
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
Employee = factory (self, name, salary) -> | |
self.name = name | |
self.salary = salary | |
self.raise = (amount) -> self.salary += amount | |
addVoice = bless (self) -> | |
self.greet = -> | |
put "Hi, my name is #{self.name}." | |
put "I currently earn $#{self.salary}." | |
alice = Employee "Alice", 50000 | |
addVoice alice | |
alice.raise 10000 | |
alice.greet() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment