Specifically, how do I define an immutable data structure. For example, I want to define:
const user = {userName: 'user name', password: 'password'}
Such that everywhere it is used or accessed in code:
- You cannot change a key
- You cannot add or delete a key
- You can only change the value associated to that key
The method of defining an immutable data structure should allow for nesting as well so that this structure is also immutable:
const user = {userName: 'user name', password: 'password', permissions: ['login', 'admin'], preferences: {favoriteColor: 'blue'} }
Why would anyone want to do this? Well, it's pretty standard in typing systems and we have real problems with data models in our JS code... we can only see the state via console.log and the structure is mutated all over the place, so devs can never see what should be a super static model and the form data becomes a mess.
Ultimately, how does one define immutable structure one way or another.
I tried everything, nothing seems to provide this need without perhaps typescript?
Via Active questions tagged javascript - Stack Overflow https://ift.tt/YlEh2Bj
Comments
Post a Comment