Transcript from the "Objects & Property Access" Lesson
>> How we feeling? We got a lot of stuff under our belt so far. We've had all kinds of values we've talked about. We talked about strings, and Boolean, and numbers, and arrays now. Look at this, we're really getting a lot of little feathers in our cap here.
[00:00:39] True, it is awesome. So [LAUGH] this is an example of an object. What do you notice? What do you notice in this, so this is the declaration and assignment of a variable JS, that assigns that variable to an object, to a piece of data of type object. What do you notice?
>> Something from the chat?
>> It has a property and value.
>> Properties and values, yes. So we're gonna talk about this. So we have these colons on each line here. And to the left of the colon is something that doesn't have quotes around it in this case, and then on the right is some kind of value.
[00:01:48] And so these are sort of, we could think of them as pseudo variables almost. Each of these little names, like abbreviation, is awesome, birth year, is kind of like a little pseudo variable within my object that points at a particular value. So, similar to how in our program, we can declare different variables to point at different values, objects let us have one value, this object that has what we call properties that point at different values.
>> Js.isAwesome, true, exactly. So this dot notation is how we access values that are kind of collected as properties of that object.
>> Do we have to break it out by line? Great question. So here, for example, I have each property and its value on a different line.
[00:04:17] We could create a new object, objy, [LAUGH] that's going to have, let's say, I don't know a different name. Objy, that's maybe confusing, Object McObjectson. [LAUGH] And maybe a, I don't know, awesomeness of 5. So in this case, I'm declaring this object all on one line. Is anything different?
[00:05:16] So what separates the different property assignments here, which character?
[00:05:44] You'll see just conventionally them separated onto different lines, just to make the code more readable, it's really all it is. So let's talk about what's happening here. When we get property values. We'll move into that in a second. When we use property value, basically when we access the value with js.name, or what have you, that is going to evaluate to the value that property is pointing to.
[00:07:50] Let's say I have an object called indecisive, where I've declared a property called lunch that points to a value sandwich. But then later, because it's indecisive, it decides that instead of sandwich, it wants lunch to be tacos. We can use the dot notation on the left-hand side of an equal sign to reassign the property to point to a different value.
[00:08:20] So if I run this code and I make my indecisive object, let's just double-check, lunch is sandwich. And if I asked for indecisive.lunch, I get, as expected, sandwich. If I now use my assignment operator equals, with the property indecisive.lunch on the left-hand side, and I run this code and assign this to tacos.
[00:08:49] Indecisive.lunch now, Has become tacos. Sandwich is forgotten, so yesterday's lunch. The interesting thing is that I can even do this to assign a new property on an existing object. So I can, for example, even though indecisive right now, it only knows about one property, it only has lunch.
[00:09:19] I can declare a new property using a dot. So I can say indecisive.snack is a new thing, a new property on the indecisive object, and give it a value. And now, if I ask for indecisive, it has two properties, lunch and snack. So are objects mutable or immutable?
>> Mutable. I can change them, I have just been doing that. So our indecisive object, it is so named as indecisive because objects are mutable, so you can change what's in them as you go.