Re:Overview of JavaScript ES6 features
by jasoncchild on Monday October 24, 2016 @09:56AM
(#12778780)
FWIW Immutable.js is not deeply immutable. Seamless-immutable however is.
> Seamless-immutable however is.
Only in development mode, by freezing the objects.
Any popular "deep freeze" utilities around?
function 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;
}
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/, been running it in production, a real pleasure to work with.