Can you please explain how the base = base[ names[i] ] = base[ names[i] ] || {}
; works?
Let's assume we're on the first turn of the loop, and so base
is window
, i
is 0
and names[i]
is "shapes". The first part to evaluate is base[ names[i] ] || {}
. This is going to resolve to window.shapes
if it exists, or an empty object if it does not.
Next, we move left to this assignment:
base[ names[i] ] = base[ names[i] ] || {};
That's going to assign to window.shapes
whatever we got from the right hand side. So if window.shapes
already exists, we assign it to itself and nothing changes. Or if it doesn't exist, we assign an empty object to it.
And moving left one more time:
base = base[ names[i] ] = base[ names[i] ] || {};
We now are going to reassign the base
variable to point to the object we just created (or the object we reused). So for the next turn of the loop, base
is no longer window
, it's window.shapes
.
At this point the loop repeats, but our variables are now one step farther along. base
is window.shapes
, i
is 1
, and names[i]
is "triangles". Since base
is now window.shapes
, we're probing and assigning to an object that's one layer deeper. So while the first time through the loop we made sure window.shapes
existed, this time we're checking if window.shapes.triangle
exists. And so it will continue, deeper and deeper into the object with each time through the loop.