3 ms·
Common misconception, const only disallows reassignment. For instance, const number = 1337; number = 10; // fails But objects/arrays are not immutable in js,
by Tankenstein 10y ago
Common misconception, const only disallows reassignment.
For instance,
const number = 1337;
number = 10; // fails
But objects/arrays are not immutable in js, so you can do this:
const person = { name: 'Dude' };
person.name = 'Dudette';
Which is perfectly valid. If you want full immutability, i recommend you check out https://facebook.github.io/immutable-js/ https://facebook.github.io/immutable-js/, been running it in production, a real pleasure to work with.
- jasoncchild 10y agoFWIW Immutable.js is not deeply immutable. Seamless-immutable however is.
- masklinn 10y ago> Seamless-immutable however is. Only in development mode, by freezing the objects.
- Roboprog 10y agoAny popular "deep freeze" utilities around?
- pitaj 10y agofunction deepFreeze(obj) { const out = Object.freeze(obj); Object.keys(out).forEach(key => { if (typeof out[key] === 'object') { out[key] = Object.freeze(out[key]); } }); return out; }
- Roboprog 10y agoI think you meant "out[ key ] = deepFreeze( out[ key ] )" in the innermost step :-) (recursive)