←back to thread

498 points azhenley | 5 comments | | HN request time: 0.436s | source
1. mcv ◴[] No.45771788[source]
I use Javscript mostly. Or Typescript actually, these days. I remember when ES2015 introduced `let` because `var` had weird scoping issues. But ever since, I barely use either of them. Everything is `const` these days, as it should.
replies(3): >>45771959 #>>45772006 #>>45772055 #
2. andsoitis ◴[] No.45771959[source]
> I use Javscript mostly. Or Typescript actually, these days. I remember when ES2015 introduced `let` because `var` had weird scoping issues. But ever since, I barely use either of them. Everything is `const` these days, as it should. reply

const prevents reassignment of the variable but it does not make the object the variable points to immutable.

To do the latter, you have to use Object.freeze (prevent modification of an object’s properties, but it is shallow only so for nested objects you need to recurse) and Object.seal (prevent adding or removing properties, but not changing them).

May people use immutable.js or Immer for ergonomic immutable data structures.

replies(1): >>45772493 #
3. rlander ◴[] No.45772006[source]
Except const is not sufficient. It will prevent the reference from being reassigned but the const can still reference a mutable object.
4. LogicHound ◴[] No.45772055[source]
`var` doesn't have weird scoping issues, it just different than other languages. `var` is function scoped, thus all var declarations are hoisted to the top of the function during execution.

This is why the single var pattern used to be recommended.

5. mcv ◴[] No.45772493[source]
That is an excellent point, and indeed a problem when debugging. When I log objects to the console, often they don't get serialized until I actually click on them, which means I don't get to see the object as it was at the time, but after a bunch of later changes.