1 00:00:00,910 --> 00:00:04,570 Now that we've seen how methods work inside of classes we're going to take a look at how he handled 2 00:00:04,570 --> 00:00:07,020 data inside them as well to get started. 3 00:00:07,020 --> 00:00:11,570 I'm going to clean up this air right here around vehicle dot honk by deleting that line. 4 00:00:11,640 --> 00:00:16,840 I'll do the same thing at the bottom this file as well all right. 5 00:00:16,850 --> 00:00:18,790 It's now back at the top on vehicle. 6 00:00:18,850 --> 00:00:21,430 I want to add in two fields. 7 00:00:21,430 --> 00:00:26,570 These are essentially going to be properties are going to reference actual values whenever you want 8 00:00:26,570 --> 00:00:28,610 to specify a property inside of a class. 9 00:00:28,670 --> 00:00:30,400 We'll just write out the name of the property. 10 00:00:30,440 --> 00:00:34,880 So maybe color and then a type annotation for that property. 11 00:00:35,270 --> 00:00:39,900 So in our case maybe color should be a string like so right now. 12 00:00:39,900 --> 00:00:45,180 As soon as you type out this line of code you might see an air around this line specifically around 13 00:00:45,180 --> 00:00:46,000 color. 14 00:00:46,170 --> 00:00:49,680 If you don't see an error in your screen looks like mine that's totally fine. 15 00:00:49,680 --> 00:00:53,960 If you do see an error it is because traditionally we want to initialize a variable. 16 00:00:54,070 --> 00:00:56,640 Anytime we declare it inside of a class. 17 00:00:56,690 --> 00:01:02,220 So right now we have declared the existence of a property or a field attached to class vehicle but we 18 00:01:02,220 --> 00:01:08,850 have not give it in and given it an initial value to do so right after the class or the property definition 19 00:01:08,910 --> 00:01:12,050 we can write in an equal sign and then some value. 20 00:01:12,060 --> 00:01:17,920 In my case I'll say red like so so this means that every time we create an instance of class vehicle 21 00:01:18,760 --> 00:01:25,360 the instance will have a color property that is a string and it's starting value will be equal to red. 22 00:01:25,390 --> 00:01:30,640 So for example down here right underneath const vehicle is new vehicle we could do a console log of 23 00:01:30,700 --> 00:01:38,930 vehicle dot color like so if we now save this lot back over to our terminal one more time and run that 24 00:01:38,930 --> 00:01:44,550 file we'll see a console log of red appear. 25 00:01:44,580 --> 00:01:48,540 Now this is just one way to initialize a variable inside of a class. 26 00:01:48,570 --> 00:01:53,430 It's also very common to want to initialize a variable using some configuration options so we passed 27 00:01:53,430 --> 00:01:56,190 to the class when we create an instance out of it. 28 00:01:56,190 --> 00:02:01,440 So for example it would be a lot nicer to specify the color right when we create a vehicle because sometimes 29 00:02:01,440 --> 00:02:03,300 a vehicle might have a color red. 30 00:02:03,300 --> 00:02:07,680 Other times it might be silver or black or orange or whatever it might be. 31 00:02:07,680 --> 00:02:12,480 So to do so we could pass in the color as an argument to vehicle right here. 32 00:02:12,480 --> 00:02:18,970 So maybe I'll do something like orange if we're going to pass in any arguments to this we have to define 33 00:02:19,000 --> 00:02:26,470 a special function inside of the class definition called the constructor so I going to write out constructor 34 00:02:26,470 --> 00:02:31,650 like so the constructor function is a very special function defined inside of a class. 35 00:02:31,710 --> 00:02:36,230 Any time that we define a constructor it will be instantly executed. 36 00:02:36,310 --> 00:02:40,510 Right when we create a new instance of the class it's like as soon as we called new vehicle right here 37 00:02:40,780 --> 00:02:42,860 the constructor function is going to run. 38 00:02:43,120 --> 00:02:47,920 And the arguments that function are going to be whatever we pass in when we create the instance. 39 00:02:48,010 --> 00:02:53,910 So in this case would be a string of orange so we would probably want to receive that as an argument 40 00:02:53,940 --> 00:02:59,910 into the constructor of something like maybe color with an annotation of string like so and then we'll 41 00:02:59,910 --> 00:03:04,460 take that value and do some initial assignment with it inside the constructor. 42 00:03:04,620 --> 00:03:11,650 So we would say this dot color is equal to color if we'd use this method of initializing color by defining 43 00:03:11,650 --> 00:03:14,830 the constructor we do not have to initialize it up here. 44 00:03:14,830 --> 00:03:17,200 So it's usually an either or decision. 45 00:03:17,200 --> 00:03:22,600 Either you are going to initialize a property on the same line that you define it or you're going to 46 00:03:22,600 --> 00:03:26,120 initialize it inside the constructor one or the other. 47 00:03:26,230 --> 00:03:28,420 In this case we'll stick with the constructor method. 48 00:03:28,540 --> 00:03:32,460 So I'm going to delete the equals read up here. 49 00:03:32,510 --> 00:03:39,020 So now if I save this file again I can run it in my terminal and I should now see get a little air here. 50 00:03:39,020 --> 00:03:42,400 Let's see argument for color was not provided my mistake. 51 00:03:42,440 --> 00:03:42,980 Let's see. 52 00:03:42,980 --> 00:03:44,540 Oh I know what it is. 53 00:03:44,540 --> 00:03:46,280 We created a car down here as well. 54 00:03:46,280 --> 00:03:47,250 Let's just comment out. 55 00:03:47,270 --> 00:03:51,770 All this stuff down here really quickly while we were working on that first class. 56 00:03:51,830 --> 00:03:56,720 This actually is a very valuable subject that we have to discuss like what we do with constructors in 57 00:03:56,720 --> 00:03:59,810 extended classes but let's just hold off on that for right now. 58 00:03:59,960 --> 00:04:04,100 So I just commented out everything in the second I the file and the Save the file and then try to run 59 00:04:04,100 --> 00:04:05,100 it again. 60 00:04:05,390 --> 00:04:05,930 And there we go. 61 00:04:05,960 --> 00:04:12,000 Now I see orange perfect now just so you know there's also a little shortcut to automate this entire 62 00:04:12,000 --> 00:04:13,770 process right here. 63 00:04:13,800 --> 00:04:17,640 Notice how we kind of have to refer to these repetitive properties multiple times. 64 00:04:17,640 --> 00:04:21,170 I see color color color all over the place. 65 00:04:21,460 --> 00:04:26,610 A equivalent way to write all this out by using a little shortcut would be to delete the implementation 66 00:04:26,640 --> 00:04:33,970 inside the constructor delete the property definition at the top and then right in front of the color 67 00:04:33,970 --> 00:04:34,470 argument. 68 00:04:34,480 --> 00:04:41,580 We could add on the word public like so public is one of those modifiers we spoke about these modifiers 69 00:04:41,580 --> 00:04:46,410 can be used not only on methods in class but on properties or fields as well. 70 00:04:46,980 --> 00:04:51,480 So the syntax you see right here is going to mean that we're going to take whatever comes in as the 71 00:04:51,480 --> 00:04:57,270 first argument to that constructor which is in this case is orange and it will be assigned as an instance 72 00:04:57,270 --> 00:05:00,820 variable of color to our vehicle that we are creating. 73 00:05:00,900 --> 00:05:07,530 So the code that you see right here is 100 percent equivalent to this right here is just slightly shortened 74 00:05:07,530 --> 00:05:11,400 up so we don't have to refer to the same color color color all over the place. 75 00:05:12,520 --> 00:05:15,890 So we're going to use this shortcut many times in the code that we write. 76 00:05:16,330 --> 00:05:20,740 If we do something like this we can then shorten up that body of the constructor function as well even 77 00:05:20,740 --> 00:05:26,880 though it's empty we still have to put it in let's try saving this flip back over run the file again 78 00:05:27,030 --> 00:05:28,280 and we'll see orange again 79 00:05:31,340 --> 00:05:31,630 all right. 80 00:05:31,630 --> 00:05:37,260 Now there's modifiers we spoke about applied to variables just the same way as they do to methods. 81 00:05:37,390 --> 00:05:44,140 So we could alternatively use the private keyword or the private modifier on color if we do so we will 82 00:05:44,140 --> 00:05:47,580 no longer be able to read that variable outside of our class anymore. 83 00:05:48,520 --> 00:05:51,450 Likewise we can also mark this thing as being protected. 84 00:05:51,790 --> 00:05:57,700 We still cannot access it outside the class but we would be able to access that field from a class that 85 00:05:57,910 --> 00:06:02,720 extends Vehicle such as car down here all right. 86 00:06:02,730 --> 00:06:07,000 I'm going to leave that variable as public like so and I'll say the file. 87 00:06:07,050 --> 00:06:08,240 We're gonna leave it like this. 88 00:06:08,250 --> 00:06:09,530 Let's take a quick pause right here. 89 00:06:09,540 --> 00:06:14,970 We're going to come back the next video and talk about how these fields work when we are using extensions 90 00:06:14,970 --> 00:06:18,350 like we are down here so quick pause and I'll see you in just a minute.