Skip to content

Instantly share code, notes, and snippets.

@wavded
Created January 24, 2012 04:02
Show Gist options
  • Save wavded/1667720 to your computer and use it in GitHub Desktop.
Save wavded/1667720 to your computer and use it in GitHub Desktop.
CoffeeScript gotchas
# Overall I like CoffeeScript, but these have bitten me.
# 1.
if cat is not "fluffy" # means if cat is false
# 2.
a = i for i in [1,2,3] # 3
b =
c: i for i in [1,2,3] # [1,2,3]
# 3.
io
.of 'context'
.on 'connection', -> # means io.of('context'.on('connection', function()...)
# 4
# works
updateCacheEntry(key, item, hash) for key, item of cache when !item.preventSubsequentCaching # all one line
# doesn't work
updateCacheEntry(key, item, hash)
for key, item of cache when !item.preventSubsequentCaching
# 5
# since arrays are generally speaking objects with numbers as keys I was thrown off by this being backwards:
for item, index in array
# because object iteration is like this:
for key, value of object
# 6
###
the whole scoping issue, i know where ruby/coffeescript stands on this issue,
i think its problematic, i must make sure i don't have a variable defined in any
outer scope otherwise it may be overwritten (but yet functions don't obey that so
could be another gotcha), feels like the reverse prob of globals (although not as bad!)
###
# 7
###
always return is beautiful for a lot of things, i wish JS proper had it (maybe w/
block lambdas). it does create a more bloat (messier to debug) when you don't want
to return and annoying to have to put 'return' at the end.
###
# 8
###
always return is a source of unexpected errors in event systems in various
frameworks (e.g. ExtJS, Vows BDD). you can write something like:
###
onadded: -> win?.show()
###
and if there isn't win, you've got a falsey value, which affects the event
propagation or handling depending on the framework, to fix you gotta add return
###
onadded: -> win?.show(); return
# 9
###
coffee handling of uncaughtExceptions in node was source of confusion for a
while, now I compile to js and use node.
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment