Transcript from the "Visualizing Object Access" Lesson
>> Let's talk about what's going on when we work with objects. Back to the whiteboard. So when I say, let indecisive equal curly braces, lunch colon, string sandwich, what is happening? Can anybody walk me through it? By now we're hopefully getting a little bit in the swing of things.
[00:00:24] You wanna walk me through it? When I say let indecisive equal object, sandwich.
>> So variables can be indecisive.
>> Okay, so we're gonna create a new indecisive.
>> Maybe I spelled it right, I don't know.
>> [LAUGH] The value is going to be the curly brackets.
[00:01:07] Well, similarly, two arrays where we had indices pointing, numbers pointing to particular values in the array. Now we have within this object, we kind of have separate pseudovariables. Which in this case, like I have a lunch property, which is sort of a pseudovariable within the object that points at a certain value.
[00:02:56] Okay, so now we can probably figure out what's gonna happen when we say, indecisive.lunch equals, tacos? [LAUGH] I think it was. When I say, indecisive, my goodness, you get the idea, .lunch equals tacos in quotes. Can anybody walk me through what they think is going to happen? Jason, would you mind walking me through it?
>> Sure, so it's going to look at the indecisive variable, follow the arrows down to lunch, and then it's going to replace sandwich with tacos.
>> Exactly, so it's now it's going to follow the arrow, find this arrow that points that's called lunch, conjure up a new string value tacos, and change this arrow, That's called lunch to point to tacos.
>> OJ, you wanna talk me through it?
>> Yep, it creates a new property for the variable indecisive, or for object indecisive, and then it creates a reference to the word shapes.
>> Right, so it's gonna follow this arrow, again, the indecisive, find that object, create a new arrow what we'll name, snack, and conjure up the value, I've asked for chips, and point that arrow at that value.
>> Could you create an array that acts like an object?
>> That's a great question.
>> Conceptually, very similar, right?
>> [LAUGH] they are conceptually very similar. Indeed, in fact, arrays are.
>> Are an object?
>> I mean, kind of.
>> Let's go to our console.
>> What's the operator that we can use to find out what kind of thing a thing is?
[00:07:27] And so here's a fun thing. This square brackets notation, this works on objects too. So what was my object called, indecisive. We said that one way we can find tacos is to say indecisive.lunch. But we can also find tacos [LAUGH] by using our square brackets and using the name of the property that we want to find.
[00:07:55] So this is very similar to how arrays let us use the square brackets with a number, which is the name of the property on the array object.
>> Objects allow us to also use the square bracket notation to access values at a named property if we give the string name of that property inside those curly braces
>> It doesn't know how to associate those individual properties to a numeric number, so you can't do indecisive zero, right?
>> Let's find out, indecisive zero. Undefined.
>> Because the object doesn't know how to parse out a numeric index?
>> Well, it's not so much that as right now my indecisive object, it has two properties, lunch and snack.