Earlier quoted context omitted.
Does JavaScript have value types where const might work the way the first author expects it to?
No. The only thing that makes const unique from let is that references declared as constants cannot be reassigned or else an error will be thrown. The exclusion only applies to the reference name itself and not to any of its properties as the article articulates.
JavaScript variables: var and let and const
11–20 of 69 posts
Re: JavaScript variables: var and let and const
#12https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
Lol that first read is fantastic. const doesn't do shit and we all know it Was a great intro, and absolutely true. Unfortunately I don't think this will catch on, although I agree with the author that it should.
Re: JavaScript variables: var and let and const
#13https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
Does JavaScript have value types where const might work the way the first author expects it to?
I think the article is a bit silly. Calling it "const" was a mistake but that's a naming issue. JS's "const" is the same as Java's "final," right? It communicates something useful.
Re: JavaScript variables: var and let and const
#14https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"
Re: JavaScript variables: var and let and const
#15https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
Re: JavaScript variables: var and let and const
#16Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…
I like that Swift has var and let. Having the first letter be different means less typing with autocompletion. I suppose when I get to use voice recognition for coding, there will also be less confusion.
Re: JavaScript variables: var and let and const
#17https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
We should use `const` to communicate that we don't intend to reassign or mutate the variable. And we should do that by default unless there's a compelling reason to do otherwise.
I know it can't be fully enforced, because JavaScript is designed for mutability of objects by default. But it is an implementation style that is almost always achievable in plain ES6 without too much trouble, and it makes more reliable software that's easier to reason about.
Re: JavaScript variables: var and let and const
#18https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
That's amazing. I was just scratching my head about const when I read the article, "so what's the purpose if I can change the value... but I guess I can't re-assign the variable with a new value .... that's it?" I still found the article incredibly informative, sadly as someone still stuck working on legacy apps it falls into "...in some near future I'll take advantage of JavaScript new stuff"
Re: JavaScript variables: var and let and const
#19https://jamie.build/const https://twitter.com/dan_abramov/status/1208369896880558080
Does JavaScript have value types where const might work the way the first author expects it to?
const myObject = Object.freeze({
myValue: "Something",
});
The problem with that is `Object.freeze` doesn't act on nested objects, so you usually have to have some other method/macro to do that for you. You might be able to do that with decorators in TS.It works on Object and Array types, but not on other primitive types. That part is frustrating. It would certainly be nice to have a single method for rendering any value immutable, whether it was the initial assignment or a built-in.
Re: JavaScript variables: var and let and const
#20Having used Kotlin for a while now, I find it really unfortunate that `const` wasn't `val` for the sole reason that it's 5 characters instead of 3. `val` would have matched nice with `let`, and my multi-variable declaration statements could have had a nice 4-space indentation while still being all aligned. I know it can be confused with `var`, but no one should ever use that now that `let` exists. One of these days I…
It just makes sense for the immutable case to be default by design instead of just by developer habit.
"var" vs "val" always were too similar for me.