4 ms·
This is why I like both Scala and Coffeescript approaches. On Coffeescript vars are created for you, and on Scala, you can't not use it (you either use var or v
by DanielRibeiro 15y ago
This is why I like both Scala and Coffeescript approaches. On Coffeescript vars are created for you, and on Scala, you can't not use it (you either use var or val, making it easy to change from re-assignable variables to final ones).
Note that nowadays going to Coffeescript from Javascript is quite easy: http://js2coffee.org/ http://js2coffee.org/
- finnw 15y agoI don't know Coffeescript - does it implicitly create a local var when there is already a global with the same name?
- Cushman 15y agoCoffeeScript is a compiler, so obviously it doesn't know anything about your runtime. It declares variables in the scope they're first assigned in; so 'global.x = y' works, while 'global = {}' compiles to 'var global; global = {};'.
- YmMot 15y agoCoffeeScript 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.
- extension 15y agoIt always creates a local var. You can only assign to globals indirectly, through properties of the global object e.g. window.foo = 123.
- colanderman 15y ago(you either use var or val, making it easy to change from re-assignable variables to final ones) I imagine that must be a headache for non-native speakers for whom "l" and "r" are indistinguishable. Imagine coding in a language developed in China with keywords "mā" and "mǎ"… most English-speaking developers would never remember which one to use because they can't distinguish between the two verbally.