Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I greatly prefer Scheme-like lexical scoping. However, JavaScript does not implement lexical scoping properly. Given the particularities of JavaScript's inadequacies, I think that the CoffeeScript design is very reasonable. It's certainly not "broken".

Lots more discussion here: https://news.ycombinator.com/item?id=3379962




Um... I believe JS does have proper lexical scoping. Could you source any evidence to the contrary?


JavaScript's lexical scoping is function-level. There is no block level lexical scoping, so variables are "hoisted" to the containing scope. This is why people use the (function(foo){...})(bar) trick to create lexical scopes, such as when looping (CoffeeScript's `do` keyword embodies this pattern). In my opinion, "proper" lexical scope implies shadowing without hoisting.


If you understand hoisting as a characteristic of JavaScript, its behavior is very predictable, and thus, hardly broken -- merely tricky if you've yet to encounter a hoisting-related issue.

That said, I find almost any issue with writing JavaScript can be easily mitigated by utilizing JSHint, and you get the added benefit of sticking with the base language, which is better because if you're writing raw JS everyday, your skills are more transferable than if you're writing CS every day if only for the fact that you can still write JS in a CS-only stack, but you can't write CS in a JS-only stack.


BTW, ClojureScript has proper lexical scope.


I'm not sure if you're telling me, or telling other folks in this thread, but for the record: I've fixed bugs in the ClojureScript compiler related to preserving the lexical scoping semantics :-) It's actually quite a tricky process and involves shadow tracking, symbol generation, self-calling function wrappers, and a bunch of other nonsense to deal with JavaScript's crummy semantics.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: