So the point of the exercise is not to re-create the glove box bit - it’s to “map” the path inside the object, in order to get to the glove box
property. Let’s try an example.
Suppose we had an object like this:
const user = {
"name": {
"first": "Bob",
"middle initial": "T",
"last": "Williams"
},
"grades":{
"EN101": 3.75,
"PSY106": 4.00,
"TM100": 3.6
}
}
Now, that user has a name
property, which we can get to via user.name
. We can use dot notation here, as the name
property is a simple word, no spaces or reserved characters. If we wanted to get to Bob’s English grade, we could use user.grades.EN101
, which would work fine.
However, if we wanted to retrieve Bob’s middle initial, user.name.middle initial
won’t work - the space in there will cause javascript to throw an error (both that middle
isn’t a property, and that initial
is not a variable, probably). So we need another mechanism to access property names with special characters (like spaces). This would be bracket notation: user.name["middle initial"]
would get us there. Note that we could do the same thing all the way down, user["name"]["middle initial"]
, but that’s a little more typing.
So, in the case of the myStorage
object, the path goes:
myStorage -> car -> inside -> glove box
… Using dot notation, you can get to inside
- but you’ll need to remember the space in glove box
and find another way there. Can you turn that path I just outlined to make an accessor for the glove box
property?