As we know, javascript can't garantee that an object has been mutated so right now the only way to garantee this didn't happen is by using other libraries (like ImmutableJS).
The problem of these libraries is they force developers to use an specific interface so the prize to guarantee this immutability is to loose all the advantages that the language offers in terms of handling Objects/Arrays. Plus, we loose the compatibility with a big amount of libraries like lodash, etc (unless we set some mappers to translate this immutable data to a plain data).
For these reasons I think we need another immutable library. In my opinion the library should:
- Should work for Objects and Arrays
- Guarantee immutability in the object/array. No property can be modified.
- Should keep same interface for reading its values. This is: dot notation or brackets notation.
- Should offer a way to guess whether we're working with a plain object/array or an immutable one.
- Could offer same interface as native objects/arrays.
- In case the native method applies an mutation. This behaviour should be changed [open discussion]
- Notify when somebody is trying to mutate a property (maybe throwing an error or just logging it)
PS: I'm currently working on a library to finish this properly. Stay tunned.
edit:
This is just an approach. Obviously, those classes need a bunch of methods to work properly.
About the Object.freeze references, the problem of Object.freeze is you don't realise when somebody tries to mutate the object and, in my opinion, that's bad.
Plus, you don't get a frozen object when you extend one. So, you have to freeze the new ones every time, what is too verbose.
edit2:
Still far from the goal but here's the repo: freezr
The benefit of using something like Immutable.js is that it optimizes the process of creating a mutated copy of the immutable object.