Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small collection of generic CoffeeScript helper functions.
# 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
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
You can’t perform that action at this time.