Skip to content

Instantly share code, notes, and snippets.

@laurengarcia
Last active December 31, 2015 15:59
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 laurengarcia/8010371 to your computer and use it in GitHub Desktop.
Save laurengarcia/8010371 to your computer and use it in GitHub Desktop.
Common Javascript patterns translated into their Coffeescript equivalents for reference.
# the not not operator
# equivalent in js:
# if (!!farts) { alert("many " + farts); }
alert "many " + farts unless not farts
# Self invoking anonymous function
do ->
return alert('anonymous foobar')
# Self invoking *named* function
# notice it executes twice --
# once when it self-invoked (with no args)
do f = ->
return alert('foo ' + arguments[0])
# ...and again only this time with an arg:
f('bar')
# Syntax for singleton module pattern
# The coffeescript version seems a little obscure
x = (->
x = ->
alert "i am x"
y = ->
alert arguments[0]
z = ->
alert "i am z, z is private!"
x: x
y: y
)()
# both x.y and x.x were returned by singleton and lose their
# privacy in the last two lines of module above
x.y "hello,"
x.x()
# Instantiate an object and give it a direct property (fart)
# and then a prototypal property (foo) that returns direct property fart
obj = {}
obj.fart = "stank"
obj::foo = (cheeseplate) ->
return false unless not cheeseplate
@fart # foo() returns fart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment