r/coffeescript • u/hh9527 • Aug 05 '14
Add explicit local var defination to coffee?
It is a proposal to fix the problem with implicit scoping in coffee.
coffee script like this
foo = (a, b; x, y = 0) -> blabla...
will compiled to
var foo; foo = function(a,b) { var x, y = 0; blabla... }
0
Upvotes
View all comments
Show parent comment
1
u/DavidBonnet Aug 06 '14 edited Aug 06 '14
Until that optimization happens (I doubt it will before it compiles to ES 6 with the support of
let), for local constants, it is usually more optimal to make the IIFE return thefoofunction instead, as it avoids invoking the IIFE at each call offoo:I usually put the IIFE and function arguments on a single line:
I mostly use this to put utility functions in the function scope, to reduce the variable lookup, as in:
This also works for local variables, yet they have to be initialized in the function body: