Object.freeze

Object.freeze allows us to disable object mutation.

  1. let movie1 = {
  2. name: 'Star Wars',
  3. episode: 7
  4. };
  5. let movie2 = Object.freeze(Object.assign({}, movie1));
  6. movie2.episode = 8; // fails silently in non-strict mode,
  7. // throws error in strict mode
  8. console.log(movie1.episode); // writes 7
  9. 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