←back to thread

498 points azhenley | 2 comments | | HN request time: 0.415s | source
Show context
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 #
1. 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 #
2. 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.