1 00:00:00,000 --> 00:00:04,826 [MUSIC] 2 00:00:04,826 --> 00:00:06,510 Hey there, welcome back. 3 00:00:06,510 --> 00:00:09,870 Congratulations on finishing the first stage of this course. 4 00:00:09,870 --> 00:00:14,040 I hope you're excited to keep learning about object oriented JavaScript. 5 00:00:14,040 --> 00:00:18,250 In the last stage, we started thinking conceptually about what objects are and 6 00:00:18,250 --> 00:00:19,660 how we use them. 7 00:00:19,660 --> 00:00:20,940 For this section of the course, 8 00:00:20,940 --> 00:00:24,900 we're going to continue our chat by talking about object literals. 9 00:00:24,900 --> 00:00:28,830 As a reminder, objects give us a way to package information about something we 10 00:00:28,830 --> 00:00:30,350 want to use in our code. 11 00:00:30,350 --> 00:00:33,430 This package is made up of a group of properties and functions, 12 00:00:33,430 --> 00:00:36,850 called methods, that work together to represent something in our program. 13 00:00:38,070 --> 00:00:41,790 Properties are like object specific variables that store information 14 00:00:41,790 --> 00:00:44,030 in a series of key value pairs. 15 00:00:44,030 --> 00:00:48,300 Methods are object specific functions that let your object do something or 16 00:00:48,300 --> 00:00:50,450 let something be done to it. 17 00:00:50,450 --> 00:00:52,970 Object literals are one way to create an object and 18 00:00:52,970 --> 00:00:57,060 they're really great when you're modeling only one single specific thing. 19 00:00:57,060 --> 00:00:57,990 Let's look at an example. 20 00:00:59,120 --> 00:01:01,200 At home I have a pet named Ernie, and 21 00:01:01,200 --> 00:01:04,770 I've created an object literal to model him in my code. 22 00:01:04,770 --> 00:01:07,760 To start, I declared a variable called ernie and 23 00:01:07,760 --> 00:01:10,830 set it equal to an empty set of curly braces. 24 00:01:10,830 --> 00:01:13,550 Curly braces are how we notate an object. 25 00:01:13,550 --> 00:01:15,350 Remember, the curly braces you use for 26 00:01:15,350 --> 00:01:18,670 objects are different than the square brackets you use to create an array. 27 00:01:19,870 --> 00:01:23,750 Inside the curly braces, I added a series of key value pairs that make up 28 00:01:23,750 --> 00:01:27,070 the three properties and one method inside this object literal. 29 00:01:28,190 --> 00:01:30,030 On the first line inside the curly braces, 30 00:01:30,030 --> 00:01:34,840 you will see the word animal, followed by a colon, followed by the string dog. 31 00:01:34,840 --> 00:01:36,410 This is a key value pair, and 32 00:01:36,410 --> 00:01:39,890 it is representing the animal property of this object literal. 33 00:01:39,890 --> 00:01:41,730 The key is the name of the property, and 34 00:01:41,730 --> 00:01:44,870 the value of the property follows after the colon. 35 00:01:44,870 --> 00:01:47,270 Property values don't have to just be strings. 36 00:01:47,270 --> 00:01:51,210 They can also be numbers, Booleans and even functions. 37 00:01:51,210 --> 00:01:53,670 In our example here, we've got three properties, 38 00:01:53,670 --> 00:01:55,720 animal, age and breed. 39 00:01:56,730 --> 00:02:00,550 An object literal's properties and methods are always separated by commas. 40 00:02:00,550 --> 00:02:04,030 Each new property or method is on its own line for readability. 41 00:02:04,030 --> 00:02:08,050 But technically, you don't have to do that for the syntax to be right. 42 00:02:08,050 --> 00:02:12,120 On the line after the breed property, you'll see another property called bark. 43 00:02:12,120 --> 00:02:16,350 But instead of a string value after the colon, there is a function declaration. 44 00:02:16,350 --> 00:02:19,440 This is one way to add methods to object literals. 45 00:02:19,440 --> 00:02:21,230 The property key is the name of the method, 46 00:02:21,230 --> 00:02:24,850 and that's the name you'll use when you want to call it. 47 00:02:24,850 --> 00:02:27,620 Putting all of these properties and methods into a package and 48 00:02:27,620 --> 00:02:30,850 attaching it to a variable is called encapsulation. 49 00:02:30,850 --> 00:02:32,530 That's an important term to remember, and 50 00:02:32,530 --> 00:02:35,220 you'll probably hear it a lot as you progress as a developer. 51 00:02:36,310 --> 00:02:39,380 Now that you know all about creating object literals, properties and 52 00:02:39,380 --> 00:02:42,830 methods, I bet you're super ready to start using these skills. 53 00:02:42,830 --> 00:02:45,920 Let's practice them a bit in a code challenge after this video. 54 00:02:45,920 --> 00:02:49,640 After that, come back and join me to learn all about how we access and 55 00:02:49,640 --> 00:02:51,350 use properties and methods.