Re:Modern Javascript: Everything you missed over the last 10...
by errantspark on Saturday May 15, 2021 @01:51PM
(#27167342)
Right but we have `let` now which solves that problem.
Re:Modern Javascript: Everything you missed over the last 10...
by titzer on Saturday May 15, 2021 @04:21PM
(#27168633)
Not completely, `let` also has a weird temporal dead zone[1], which is basically a dynamic check.
[1] https://www.freecodecamp.org/news/what-is-the-temporal-dead-...
Re:Modern Javascript: Everything you missed over the last 10...
by IshKebab on Sunday May 16, 2021 @03:55AM
(#27171996)
That's a good thing, and I'm not sure what you mean about it being a dynamic check.
Re:Modern Javascript: Everything you missed over the last 10...
by titzer on Sunday May 16, 2021 @01:38PM
(#27175428)
It's a dynamic error if you access a let-bound variable before it is actually initialized. In most other languages that have lexically-scoped declarations, it simply would not be visible. So I think JS's choice is actually a regression.
Re:Modern Javascript: Everything you missed over the last 10...
by austincheney on Saturday May 15, 2021 @01:48PM
(#27167312)
To be more specific the term is hoist which means the declarations are pushed to the top of the scope at run time for maximum availability, and var is function scoped instead of block scoped. Block scoping is often more specific because a function body is a block as well.
Re:Modern Javascript: Everything you missed over the last 10...
by hwillis on Saturday May 15, 2021 @01:54PM
(#27167365)
var is a terrible example of that. Just don't use it.
A good example would be the scoping in for loops and maybe arrow functions. For loops are goofy but virtually never an issue, and arrow functions are awesome.
I assume you mean over the alternative, dynamic scoping like in e-Lisp. But you realize that JavaScript's scoping rules are entirely wonky, e.g. that "var" declarations are lifted to the function level?