Hi. Yeah, I remember thinking this for similar data structures when I first came across them.
But it's not the case.
The key thing is that an array** doesn't contain another object, but the memory address of that other object.
In your diagram, you're representing an array as holding another array.
It's not. Each array** is holding a reference to an object; a reference being a high-level representation of the memory address which contains the first byte of that object. (Simplifying a few things, but this simplification is fine for this concept.)
Further, each variable, so post1 and post2, don't hold the object, but a memory address or reference to that object. Variables are different than objects, even though we often use the terms synonymously; variables hold the location of an object.
Sort of like
- a letter (those are things that existed before email)--analogous to a variable--
- has an address--analogous to a memory address, i.e., a location--
- that refers to a house, condo or apartment--analogous to an object.
A letter doesn't actually have the house on it; just the address or location of, or a reference to that house, apartment or condo (or whatever lot or building is at that address).
So, let's re-do your diagram.
0x101 and 0x338 will represent memory addresses 101 and 338 respectively; I chose the numbers randomly, as pretty much the only important thing is that they're different (and not necessarily decimal )***.
post1 contains the mem. address, or reference, 0x101, post2 contains 0x338.
At 0x101 is the post1 object, and at 0x338 is the post2 object.
So, we'll have
post1 : 0x101
post2 : 0x338
This separation between a variable and the object to which it refers is critical among many languages to understanding linked lists, trees, etc., stack vs. heap. Why many believe C and assembly are still a critical languages to learn, even though they're not only used for very low-level things..
I've glossed over some things****, and am not 100% how JS does this, but other languages at a high-ish level implement it this way, and the things I glossed over are probably not too important.
** Well, more accurately each array's element has that reference.
*** Another important thing is that their objects don't in anyway overlap; so, the memory taken up by the post1 object is separate from the post2 object's memory.
**** Namely, but a bit briefly, the fact that connectionsTo isn't modeled accurately (hint: properties that refer to objects can have a similar level of indirection,via a mem. address, when compared to array elements and variables), and a brief explanation of why x, y and postNum are different (probably ... not 100% how JS treats them), and similarly an explanation of why some arrays, properties and variables likely do not hold a memory address ... be aware, but I'd say ignore for now. Of course, if you're curious, no need to heed that advice, and feel free to ask.