4 ms·
A better question is how useful is let at all? Having two different variable scoping rules with no way to know which one a variable is using without backtracing
by protonfish 11y ago
A better question is how useful is let at all? Having two different variable scoping rules with no way to know which one a variable is using without backtracing sounds like a maintenance nightmare. I understand what is it good for, but it seems like we are replacing a small problem with a bigger problem.
An even better question is why not get rid of var and make every variable local to its parent function? To alter globals, use the window object explicitly.
- serve_yay 11y ago> better question is how useful is let at all? Having two different variable scoping rules with no way to know which one a variable is using with backtracing sounds like a maintenance nightmare. I agree, but I think the solution is to stop using `var`, the question is how useful `var` really is. `let`'s scoping is what it appears to be, `var`'s is not. So `let` is the better choice.
- protonfish 11y agoI disagree. Maybe you think this because you are used to another language, but once you get how var works, it's quite clear. Just because some other language does it, does not mean it is unquestionably good.
- serve_yay 11y agoOn the contrary, I pretty much only write JS and have for years now. And I think `var` has warped our brains, not the other way around.
- tangent128 11y agoIf Javascript didn't have closures, then var's scoping rules would largely be academic. But in practice, there are many cases when I have to create multiple callbacks in a loop; half the time I forget that, in Javascript, declaring a variable inside a loop does not actually create a variable local to the loop, and so have to keep debugging why all my callbacks are behaving exactly the same. The let statement appears to adopt Lua-style lexical scoping, which will be a breath of fresh air once I can rely on browser support.
- hrjet 11y ago> To alter globals, use the window object explicitly. JS is used in non-DOM contexts as well. Though, of course, if that big a change is being made, a generic substitute for `window` could also be specified.
- reubenmorais 11y ago> a generic substitute for `window` could also be specified Isn't that just |this| at the top level?
- singlow 11y agoNot necessarily the _top_ level if you are referring to scope. `this` will be the default global unless another context is defined, whether you are at the top level or inside a function. In node, there is a name for the default global: "global", which is also the default context. So node gives it a "generic substitute" as the OP suggested. Most people use "this" to make assignments to it, rather than the "global" name, since it is usually accessed from the default context.
- deleted 11y ago[deleted]
- untog 11y agoI think the answer is to just use let. In an ideal world they'd just change the behaviour of var to something sensible, but they (wisely) wanted to retain backwards compatibility. const is a different case, but IMO there's no need to use var when let exists.