4 ms·
This seems to be in agreement with GP: > You should be using const for everything besides variables that need to get re-assigned.
by wtetzner 8y ago
This seems to be in agreement with GP:
> You should be using const for everything besides variables that need to get re-assigned.
- hajile 8y agoYou wouldn't start coding python for the first time without some research. You wouldn't for Java, ruby, C, bash, or any other language you can name. JS is the one language that very few people bother to learn. They just start writing and see what works. For these people, a const that isn't const isn't a very good idea. Even among the good JS devs I know, most haven't learned this until it actually bit them in production because most books about JS don't mention the problem. The TDZ (temporal dead zone) is a direct result of adding `const` to the language and it complicates everything (plus has performance implications). A feature that doesn't do what it seems to do while complicating everything else about the language is a textbook example of bad design. There are several people who agree in hindsight (I believe Brendan Eich is among them).
- anonytrary 8y ago`const` "seems to do" this: prevent variables from getting re-assigned. That's all one needs to know to understand the associated pitfalls.
- BrendanEich 8y agoFrom C99 and C++, also Java, `const` does not affect the bound value. It's a declaration keyword, so it affects only how to bind the declarator name or names that follows immediately. This is all stating the obvious, but expecting effects on the initialiser will lead only to frustration, on two counts: 1. JS is in the C/C++/Java lineage syntactically. Ok, that is a deeper design flaw, but Netscape management to keep Sun on board said "Make It Look Like Java". 2. If you want declarations to affect captured expressions, you'll soon want a type system if not immutability controls and defaults. IOW, at least Scheme-like binding rules and set! as special form to use when mutating. Again the ships sailed 23+ years ago, and in truth for "Mocha's" target audience, immutable-by-default would have been harder to use. "They just start writing and see what works" => a primary design goal. Flawless Victory! :-) Seriously, the Scheme in the browser baby was killed in the cradle; the recruiting bait used to get me on board was snatched away because of the Sun/Netscape Java deal. All is not lost, as JS grows better special forms (but for backward compatibility must keep old ones). Among these, `const` is least better but still useful. What's more, those who advocate mass-market immutable-by-default are still working to make it usable in the large (e.g., in Rust). Pedagogy helps, but also has not delivered the goods yet. We are living through the full experiment to refine and confirm that i-b-d is best.