←back to thread

498 points azhenley | 1 comments | | HN request time: 0s | source
Show context
stevage ◴[] No.45768231[source]
In JavaScript, I really like const and have adopted this approach. There are some annoying situations where it doesn't work though, to do with scoping. Particularly:

- if (x) { const y = true } else { const y = false } // y doesn't exist after the block - try { const x = foo } catch (e) { } // x doesn't exist after the try block

replies(4): >>45768310 #>>45768515 #>>45768529 #>>45771225 #
latexr ◴[] No.45771225[source]
JavaScript’s `const` has the bigger issue that while things can’t be reassigned, they can still mutate. For example:

  const myArray = [1,2,3]
  myArray.push(4)
  myArray // [1, 2, 3, 4]
replies(1): >>45771533 #
stevage ◴[] No.45771533[source]
In what way is that an issue?
replies(1): >>45773670 #
maleldil ◴[] No.45773670{3}[source]
Because this isn't immutability. The goal is to have a way to define an object that will never change after initialisation, and JS's const isn't it.
replies(1): >>45776639 #
stevage ◴[] No.45776639{4}[source]
Clearly that isn't the goal.
replies(1): >>45778778 #
1. maleldil ◴[] No.45778778{5}[source]
By "the goal", I mean TFA's, not JS's.