4 ms·
CoffeeScript doesn't allow you to shadow variables in enclosing scopes. The compiler will declare the variable in the "outer-most" scope in which is is used and
by YmMot 15y ago
CoffeeScript doesn't allow you to shadow variables in enclosing scopes. The compiler will declare the variable in the "outer-most" scope in which is is used and then any uses in enclosed scopes refer to that one.
In this case as long as OP was not using the variable in an enclosing scope, it would have been made local to the function.
- mmahemoff 15y agoAnd CS also wraps modules in closures (by default), so an outermost-scoped variable is still local to the script, whereas it would be global to all scripts in vanilla JS.
- geraldalewis 15y agoThis is true for the most part, but there's a subtle edge case. Within a nested function, variables used in loops will redeclare within the inner function: -> outerVar = "outer" -> # this will output "undefined" # as CoffeeScript has redeclared it for the loop: console.log outerVar 0 for outerVar in [0]
- jashkenas 15y agoHuh -- that looks like a (new) bug to me. We should reuse the external declaration.