Hacker News new | ask | show | jobs
by boundlessdreamz 5287 days ago
Thanks for this! Way better explanation than the blog post :)

Is there anyway to make the scope explicit in coffeescript?

2 comments

No. That's the crux of the complaint. In plain Javascript, you can use "var x" to scope x to the current scope (and in 1.7, you can use 'let' to scope to the current block, not just to a function), but there's no corollary in Coffeescript, since Coffeescript manages scoping for you automagically. It's a convenience 99% of the time, but the 1% of the time that it's not, it's a giant pain in the ass. If there were something like var/let/local, you could solve the problem manually, but as the Twitter exchange indicated, there is no plan to add it.

The solution/workaround is "use descriptive names and avoid polluting scopes", but the reality of software development is that you're eventually going to cross those wires, and it's going to make you crazy until you figure out what happened.

Yes, create the variable as a function argument.
While that does allow you to use a locally scoped variable (even one that shadows an outer variable), it's still not explicit scoping.