Object.freeze
Object.freeze
allows us to disable object mutation.
let movie1 = {
name: 'Star Wars',
episode: 7
};
let movie2 = Object.freeze(Object.assign({}, movie1));
movie2.episode = 8; // fails silently in non-strict mode,
// throws error in strict mode
console.log(movie1.episode); // writes 7
console.log(movie2.episode); // writes 7
One problem with this pattern, however, is how much more verbose our code is and how difficult it is to read and understand what's actually going on with our data with all of the boilerplate calls to Object.freeze
and Object.assign
. We need some more sensible interface to create and interact with immutable data, and that's where Immutable.js fits in.
Object.freeze
is also very slow and should not be used with large arrays.
原文: https://angular-2-training-book.rangle.io/handout/immutable/javascript-solutions/object_freeze.html