3 ms·
With JS, until recently, it was nearly impossible to design immutable API's. Everything takes JSON based objects which have no field accessors. All the objects
by throwaway189262 6y ago
With JS, until recently, it was nearly impossible to design immutable API's. Everything takes JSON based objects which have no field accessors. All the objects underneath are just hashmaps with string keys.
When all your objects are hashmaps you can't really make fields immutable. Even if you wrote getters, someone can just reassign the function reference.
Even if you check your fields in incoming object parameters, somebody can randomly add more fields that you don't know about. Until recently you couldn't stop people from randomly adding fields to your objects
Nowadays JS supports stuff like freeze thaw which makes it easier
- Ndymium 6y agoObject.freeze() has been available since IE 9, Firefox 4, Chrome 6 etc. Not really "recently" in web terms.
- jimmaswell 6y agoYou can mess with private fields with reflection in other languages too. If someone's bent on bypassing your API you're not going to stop them.
- winstonewert 6y agoYou didn't have language enforced immutability. But you still could have convention enforced immutability. If you don't provide apis that mutate your objects and document that mutating is not supported, you get almost all of the benefits of immutability.