2 ms·
In JavaScript const is not immutable. const foo = {}; foo.bar = 'baz'; This is valid, you cannot reassign foo, but you can mutate it. Native immutabl
by Touche 12y ago
In JavaScript const is not immutable.
const foo = {};
foo.bar = 'baz';
This is valid, you cannot reassign foo, but you can mutate it.
Native immutables in JavaScript would be great, but would be hard to pull off given prototypical inheritance.
EDIT: Now const + Object.freeze gets you close to immutable. I'm not sure what that means for inherited functions, can the parent prototypes not be mutated either?
- acjohnson55 12y agoYeah, this is also true of the `val` statement in Scala too. I suppose though that in Scala, one would probably make all data members of classes `val` as well, whereas in Javascript, there's no in-line syntax for constant object properties (Object.freeze being a bit verbose to use everywhere). But even that limited form of enforced immutability is helpful. I personally find it easier to understand a complex function when I can assume that no names are being rebound.