1 00:00:01,890 --> 00:00:07,980 Welcome back to Learn to Code and JavaScript in this lecture, we are going to start talking about objects. 2 00:00:08,820 --> 00:00:11,800 Objects are just a way of grouping data together. 3 00:00:13,470 --> 00:00:15,000 So how do we define an object? 4 00:00:15,480 --> 00:00:23,310 An object is defined very similarly to how in arrays defined so we can group data together into one 5 00:00:23,310 --> 00:00:32,820 package and we use curly braces to define an object literal with commas separating the values. 6 00:00:33,630 --> 00:00:35,530 The difference between an object and an array. 7 00:00:36,090 --> 00:00:39,770 Is that the items in an object have a name. 8 00:00:40,980 --> 00:00:46,320 So this object has a name and age and a signed up attribute. 9 00:00:47,380 --> 00:00:53,830 So what my object equals, and you start with a curly brace name Colin, and of course, a string Bob 10 00:00:54,610 --> 00:01:01,570 comma, the next item, age column 12 comma, signed up colon. 11 00:01:01,720 --> 00:01:02,230 True. 12 00:01:03,680 --> 00:01:09,380 So that defines an object so the object owners can be accessed by their name. 13 00:01:11,940 --> 00:01:16,530 So I can say council that log my object, that name my object, that age. 14 00:01:18,200 --> 00:01:25,140 So I just can also be accessed using a string name with square brackets, kind of like an array, so 15 00:01:25,180 --> 00:01:31,490 I can say my object square bracket, string name, my object, square brackets, string age. 16 00:01:32,390 --> 00:01:34,220 So there's two ways to access the objects. 17 00:01:36,250 --> 00:01:37,910 There's also a null object. 18 00:01:37,910 --> 00:01:39,830 And you may have heard me refer to null. 19 00:01:40,340 --> 00:01:43,660 So that just means an object that has no value, that has nothing in it. 20 00:01:47,980 --> 00:01:53,110 Objects can store any type of data, including other objects, arrays and functions. 21 00:01:53,140 --> 00:01:58,600 When we get to functions, we'll look at that so I can say let my object equals name. 22 00:01:59,030 --> 00:02:02,260 Sue list an array. 23 00:02:02,270 --> 00:02:02,800 Twenty two. 24 00:02:02,800 --> 00:02:04,780 Forty four, sixty six, eighty eight. 25 00:02:07,390 --> 00:02:10,770 And then sub subject some other object. 26 00:02:11,920 --> 00:02:17,230 So let's take a look at some examples of how objects are built in visual studio code. 27 00:02:19,300 --> 00:02:19,590 All right. 28 00:02:19,600 --> 00:02:29,440 So let's build as an example, an employee object so we'll say what the equals. 29 00:02:30,670 --> 00:02:34,630 And if it's a longer name, we always put it on multiple lines. 30 00:02:34,850 --> 00:02:38,640 If it's a longer set of attributes in the object sequence. 31 00:02:38,650 --> 00:02:39,370 A name 32 00:02:42,460 --> 00:02:42,970 Bill. 33 00:02:47,000 --> 00:02:53,910 No, no, we use Kemel case on our names and of an object as well, and we'll say his employee number 34 00:02:53,910 --> 00:02:55,800 is one, two, three, four or five. 35 00:02:57,270 --> 00:02:58,680 I'm just going to be a strength, 36 00:03:03,270 --> 00:03:07,950 let's say, of four time. 37 00:03:10,110 --> 00:03:10,620 True. 38 00:03:14,200 --> 00:03:27,790 Salary for, say, ten thousand and then will create an array called shifts, which just represents 39 00:03:27,790 --> 00:03:28,900 the days and the week. 40 00:03:29,890 --> 00:03:32,110 So we'll start on Sunday. 41 00:03:32,120 --> 00:03:40,540 So we'll say Sunday, zero eight hours on Monday, eight hours on Tuesday, off on Wednesday, Thursday 42 00:03:40,540 --> 00:03:44,230 eight hours, Friday four hours and Saturday, four hours. 43 00:03:45,100 --> 00:03:46,930 And that's good enough for a quick example. 44 00:03:46,930 --> 00:03:48,160 So we'll leave it like that. 45 00:03:51,040 --> 00:03:52,900 And you need a semicolon at the end. 46 00:03:54,520 --> 00:03:55,680 And let's just log it out 47 00:03:59,500 --> 00:04:00,670 so we'll see what it looks like. 48 00:04:11,990 --> 00:04:20,670 So Output's, it's kind of like a race get output, we can have a little triangle and expand it so we 49 00:04:21,440 --> 00:04:28,610 know full time our name, see the sort of the attributes alphabetically and then our shifts, I can 50 00:04:28,610 --> 00:04:30,020 open those up and see what those are. 51 00:04:30,960 --> 00:04:33,110 And then let's just manipulate this a little bit. 52 00:04:34,520 --> 00:04:40,220 So then we'll say give Bill a raise. 53 00:04:42,200 --> 00:04:44,780 Saddam will say that salary. 54 00:04:47,210 --> 00:04:54,160 And so times equals one point of five dollars, going to get a five percent raise. 55 00:04:55,100 --> 00:04:58,940 So let's actually make those salary be an hourly salary. 56 00:05:00,010 --> 00:05:01,880 And so let's just make it ten dollars an hour. 57 00:05:03,350 --> 00:05:05,870 So now let's calculate bills, weekly pay. 58 00:05:11,670 --> 00:05:24,620 So we'll say let total pay equals zero and we'll say for S of we use the four of the loop and say that 59 00:05:24,630 --> 00:05:25,320 shifts 60 00:05:28,950 --> 00:05:38,250 and we will say total pay equals X times dot salary. 61 00:05:41,850 --> 00:05:45,000 And then we'll say cancel that log. 62 00:05:47,780 --> 00:05:48,710 Hey, Bill. 63 00:05:53,150 --> 00:05:53,900 Total pay. 64 00:06:03,580 --> 00:06:06,100 This is not define let us. 65 00:06:13,900 --> 00:06:16,150 So I paid off 40 to. 66 00:06:18,900 --> 00:06:28,290 And still looking out there on the bottom, so Bill gets paid for it, too, but let's say we've given 67 00:06:28,290 --> 00:06:29,510 Bill a raise. 68 00:06:30,270 --> 00:06:34,950 So Bill's salary is ten dollars and 50 cents after his raise. 69 00:06:35,100 --> 00:06:36,770 And then we calculate the pay. 70 00:06:37,380 --> 00:06:40,260 But let's say we were also going to cut Bill's hours. 71 00:06:42,520 --> 00:06:52,290 So say cut Bill's hours now we can't use an envelope to go through and subtract the hours we need, 72 00:06:52,300 --> 00:06:57,960 use a regular for loop, so we'll save for I equals zero. 73 00:06:58,000 --> 00:07:03,250 I was then and that shifts that the length. 74 00:07:05,670 --> 00:07:07,170 My post was. 75 00:07:10,570 --> 00:07:23,230 And then we'll say Abduh shifts, subi times equals are going to boy 10 percent cut point, not a 10 76 00:07:23,230 --> 00:07:24,480 percent cut in hours. 77 00:07:25,420 --> 00:07:29,440 So each one of these days is going to get cut by 10 percent. 78 00:07:30,940 --> 00:07:32,730 So now let's run this and see what we got. 79 00:07:38,600 --> 00:07:45,020 So now Bill is going to be paid thirty seven dollars and 80 cents and I realized an error early on, 80 00:07:45,170 --> 00:07:48,200 we should have been doing it plus equals here all along. 81 00:07:50,900 --> 00:07:52,970 So we've been underpaying poor Bill. 82 00:07:55,410 --> 00:08:04,080 So three hundred and two dollars and 40 cents, so if we go back and just cut that out, this cut just 83 00:08:04,080 --> 00:08:05,100 to see what that was. 84 00:08:08,440 --> 00:08:14,620 So previous three hundred thirty six dollars, so that's a brief introduction into defining and using 85 00:08:14,620 --> 00:08:15,190 objects.