3 ms·
The idea is not to use objects. You can do everything you do with objects using plain data and working on it using functional style. Seeing how Js objects are a
by yogthos 8y ago
The idea is not to use objects. You can do everything you do with objects using plain data and working on it using functional style. Seeing how Js objects are already effectively maps with keys on them, it's not as big a jump as it may seem.
- supermatt 8y agowhat is plain data and how is it structured? edit: seems like you updated as i posted. i’m just trying to understand how you do this without using an object. you mention use of a map, but that is also an object. also, by copying a map i still access the original values, which is specifically what this article is implemented to avoid? i may be over my head here, i’m just trying to understand the “correct” way of doing this in javascript.
- yogthos 8y agoJSON is an example of plain data. You structure it by nesting maps and lists of items. For example, I work with medical data and here's an example of a structured patient resource in JSON format https://www.hl7.org/fhir/patient-example.json.html https://www.hl7.org/fhir/patient-example.json.html
- supermatt 8y agoif i parse that in javascript it becomes an object. if i want to copy that object it copies the reference to the object and references to the same properties. if i modify it, it then it modifies the original. the best i can do is to create a new object and copy the references over or use it as a prototype, both of which would be considered immutable. nested objects would need to be dealt with in the same way. as i understand, that’s what this lib is doing?
- yogthos 8y agoYou wouldn't parse the data into objects. Instead, the immutable data structure libraries have their own implementation of maps, lists, and so on. These are the data structures you'd be using. And of course, if you're using a language like ClojureScript then it's just how things work out of the box.
- supermatt 8y agoi appreciate what you are saying about these immutable data libs, but i wasn’t aware they parsed and manipulated json directly. it sounds horrendously inneficient to me, but i’ll do some further investigation. thanks for the heads up
- yogthos 8y agoThat's not what I was suggesting these libs do. You receive JSON as text over the wire. Instead of parsing it into native Js objects, it's instead parsed into alternative implementations of immutable data structures. There's nothing inefficient about it.
- supermatt 8y agothanks. i hadn’t looked into the detail of these libs and had made some incorrect assumptions. will definitely look at their usage for a project historically suffering with side effects on the data.