The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.
I think it's both things really. Python, for instance, is much saner than Coffeescript yet doesn't distinguish between defining and setting a variable. It gets away with it because variables are function scoped (like Javascript). This creates other issues though such as not being able to write to outer-scoped variables without an additional construct. Distinguishing variable declarations from setting existing variables seems like the cleanest way to do it to me, and it supports arbitrary scoping. I also think it conveys the intent of the code better and allows for better error checking.
View all comments
Show parent comment
2
u/didroe Jul 26 '13
I think it's both things really. Python, for instance, is much saner than Coffeescript yet doesn't distinguish between defining and setting a variable. It gets away with it because variables are function scoped (like Javascript). This creates other issues though such as not being able to write to outer-scoped variables without an additional construct. Distinguishing variable declarations from setting existing variables seems like the cleanest way to do it to me, and it supports arbitrary scoping. I also think it conveys the intent of the code better and allows for better error checking.