0:00:01.333,0:00:03.803 I've written a program to tell you juicy details about 0:00:03.803,0:00:07.469 Winston, but not too much because Winston likes to keep some 0:00:07.469,0:00:11.870 mystery. That's just the Winston way. So, let's 0:00:11.870,0:00:15.334 see how I did this program. I created some variables at the top to store bits of 0:00:15.334,0:00:20.034 information about him. The first variable holds a number - his age - the second variable holds 0:00:20.034,0:00:23.955 a string - his eyes - the third variable holds an array of strings, 0:00:23.955,0:00:27.907 which are things he likes doing, and the last two variables hold 0:00:27.907,0:00:31.566 strings which describe where he was born. Then 0:00:31.566,0:00:35.940 down here, I wrote each of these out using the text command and just using the variable 0:00:35.940,0:00:39.699 name. And of course for the array, I have to access each element of the array 0:00:39.699,0:00:43.450 using bracket notation. Cool. Now, 0:00:43.450,0:00:48.636 all five of these variables are describing information about the same thing: Winston 0:00:48.636,0:00:51.598 But the variables don't know that they're about the same thing 0:00:51.598,0:00:55.502 And, you know, in Javascript, when you wanna 0:00:55.502,0:00:59.289 store multiple pieces of information that are related, we have a better way of 0:00:59.289,0:01:03.125 storing them that's really cool, and it's called an object. 0:01:03.125,0:01:06.795 So what it means is that instead of 5 variables, we can have a single 0:01:06.795,0:01:11.623 variable that stores all this information, which is pretty cool. 0:01:11.623,0:01:15.169 Let's try it out with Winston's information. First, we'll declare the 0:01:15.169,0:01:18.837 variable and we'll call it Winston. And then 0:01:18.837,0:01:23.399 we put an open curly bracket - make sure it's curly, not square - 0:01:23.399,0:01:27.107 and a semicolon. So we've created an object 0:01:27.107,0:01:31.047 but it has absolutely no information inside of it. 0:01:31.909,0:01:35.668 So, to add a bit of information, we need to add properties. 0:01:35.668,0:01:41.276 And each property is a key and a value. For example, age would be age: 19 0:01:41.276,0:01:43.789 Okay. And then for eyes, we're gonna add a 0:01:43.789,0:01:48.039 comma, and then eyes: "black". Okay, cool, 0:01:48.039,0:01:52.742 so now Winston has two properties inside the object. 0:01:52.742,0:01:55.607 Uh, for likes, we can just go likes: and then I'll 0:01:55.607,0:02:00.076 just copy paste this from up here... and 0:02:00.076,0:02:04.000 very nice. So let's look at this. Winston has three properties 0:02:04.000,0:02:07.527 Every property has a key, which is what's on the right 0:02:07.527,0:02:12.796 hand side, and a value, which is what's on the left-hand side. 0:02:12.796,0:02:15.632 For the key, it should follow the same rules as Javascript 0:02:15.632,0:02:20.099 variable names. No spaces, start it with a letter, all that 0:02:20.591,0:02:22.960 For the value, it can be any type of value 0:02:22.960,0:02:27.124 we've seen so far. It could be number, it could be a string, it could be an array 0:02:27.124,0:02:33.373 It could even be a boolean, so we could add isCool: true, of course 0:02:34.126,0:02:38.398 In fact, the value could even be another object. So, 0:02:38.398,0:02:42.709 BirthCity and BirthState. Those really are bits of information about the same 0:02:42.709,0:02:46.616 thing, which is a single location. And, so I think it be make 0:02:46.616,0:02:51.042 more sense if we stored it as an object. I'll add another key, birthplace, 0:02:51.042,0:02:54.836 and then for the value I'm gonna put my curly brackets again 0:02:54.836,0:02:58.376 and then inside I'll have key for city, 0:02:58.376,0:03:02.287 "Mountain View", and then state, 0:03:02.287,0:03:06.766 "California". Great! So now you can see, you can really 0:03:06.766,0:03:10.480 store very rich information inside an object. 0:03:11.033,0:03:15.459 All right. So now that we have this nice object that describes all this information about Winston, 0:03:15.459,0:03:18.783 let's try deleting those old separate variables 0:03:18.783,0:03:23.066 that didn't know about each other. Okay 0:03:23.066,0:03:27.039 Um, uh oh! Uh so now we've got an error. And that's because our 0:03:27.039,0:03:30.739 text commands are referencing the old variables. We need to update 0:03:30.739,0:03:34.502 them to use information from the object instead. Let's 0:03:34.502,0:03:38.218 start with just commenting out the last three so that we can 0:03:38.218,0:03:41.872 do one at a time. Okay. So it says WinstonAge 0:03:41.872,0:03:46.396 right now. What we need to do is replace that, so we'll type 0:03:46.396,0:03:50.293 winston, cause that's the variable name. Notice if we 0:03:50.293,0:03:53.834 just leave it like that, it says object object. That's really 0:03:53.834,0:03:57.369 gross. That's Javascript telling us that we're trying to turn an entire 0:03:57.369,0:04:02.109 object into a string value. But instead, we really just 0:04:02.109,0:04:06.134 wanna access only the age inside of it. So what we do is we put a 0:04:06.134,0:04:11.063 dot, and then we write the property key, which is "age". 0:04:11.063,0:04:14.897 Tada! We have the age. We call this "dot notation," 0:04:14.897,0:04:18.284 which is where we write the variable name for the object, and then a dot, 0:04:18.284,0:04:21.791 and then the property key. Okay, so we can 0:04:21.791,0:04:26.244 go and do more now. Let's uncomment this, and then instead of Winston 0:04:26.244,0:04:29.877 eyes, we'll just say winston dot eyes, and then 0:04:29.877,0:04:34.074 for this one, it'll be winston dot likes, 0:04:34.074,0:04:37.741 and then winston dot likes one, and then 0:04:37.741,0:04:42.501 for this last one, it's a little more complicated because 0:04:42.501,0:04:45.451 it's an object inside an object. So we're gonna say 0:04:45.451,0:04:48.784 winston, and then dot, birthplace, 0:04:48.784,0:04:53.288 but if we do that it's still just the whole object so then we have to say dot, 0:04:53.288,0:04:57.455 city. Okay let's do this here... winston, 0:04:57.455,0:05:01.572 dot, birthplace, dot, state. 0:05:01.572,0:05:05.791 Tada! So it's really cool cause you can just reach down inside the objects 0:05:05.791,0:05:09.416 that are in the objects. All right 0:05:10.863,0:05:13.919 Awesome. So, as you can see, 0:05:13.919,0:05:17.597 objects are a great way to store a bunch of related bits 0:05:17.597,0:05:21.406 of information about an object, and then be able to access it later. 0:05:21.406,0:05:25.406 And when you keep going, you're gonna find out just how awesome objects are!