Last active
December 31, 2015 15:59
-
-
Save laurengarcia/8010371 to your computer and use it in GitHub Desktop.
Common Javascript patterns translated into their Coffeescript equivalents for reference.
This file contains hidden or 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
# 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