1 00:00:02,250 --> 00:00:07,510 So that's classes properties and inheritance with typescript. 2 00:00:07,560 --> 00:00:13,860 And as I mentioned to these features also generally are supported in JavaScript in modern javascript 3 00:00:14,280 --> 00:00:20,880 of course with the exception of type assignments and also with the exception of protected private public 4 00:00:21,090 --> 00:00:24,810 these keywords are really only available in typescript. 5 00:00:24,810 --> 00:00:32,160 Now it's not all what you can do with classes though when ever need feature which you also have in vanilla 6 00:00:32,190 --> 00:00:38,660 javascript and which is also supported in typescript R getters and setters. 7 00:00:38,670 --> 00:00:46,530 Now let's see that here on the accounting department for example let's say there we have a lost report 8 00:00:46,800 --> 00:00:51,510 property which is a private property and it's of type String. 9 00:00:51,660 --> 00:00:53,130 So we have that. 10 00:00:53,370 --> 00:01:03,230 And then here we initialize this to be our reports property or this argument we're getting here and 11 00:01:03,230 --> 00:01:03,740 there. 12 00:01:03,970 --> 00:01:09,770 The first value which of course is basically undefined if we pass in an empty reports array. 13 00:01:09,920 --> 00:01:18,260 Now when we add a report then don't want to set this last report equal to text which is what we add 14 00:01:18,260 --> 00:01:20,240 to our report's array. 15 00:01:20,480 --> 00:01:26,250 Now last report is private so we can access it from inside this method. 16 00:01:26,270 --> 00:01:30,280 But we won't be able to access it from outside with the dot notation. 17 00:01:30,290 --> 00:01:36,980 Now we can add a getter to still make it accessible a getter is basically a property where you execute 18 00:01:36,980 --> 00:01:40,810 a function or a method when you retrieve a value. 19 00:01:41,000 --> 00:01:45,220 And that allows you as a developer to add more complex logic here. 20 00:01:45,260 --> 00:01:49,200 You create a getter by using to get keyword and then any name of your choice. 21 00:01:49,250 --> 00:01:54,140 Typically closely related to the property you're trying to control the access to. 22 00:01:54,230 --> 00:01:58,130 For example here we could name it most recent report. 23 00:01:58,220 --> 00:01:59,310 Anything like that. 24 00:01:59,390 --> 00:02:01,700 Now import does is defined like a method. 25 00:02:01,700 --> 00:02:04,440 So you add parentheses and then curly braces. 26 00:02:04,540 --> 00:02:11,580 And now in here you have to return something that's important getter method has to return something. 27 00:02:12,020 --> 00:02:18,290 So here we could return this last report and then we kind of encapsulate this because this is publicly 28 00:02:18,290 --> 00:02:19,190 accessible now. 29 00:02:19,550 --> 00:02:22,750 But often you want to have more complex logic here. 30 00:02:22,760 --> 00:02:26,230 So here we could check if we have a value here. 31 00:02:26,230 --> 00:02:27,550 So if this is true. 32 00:02:27,640 --> 00:02:30,980 If it's not undefined then we want to return last report. 33 00:02:31,270 --> 00:02:34,480 Otherwise you might want to return some different logic. 34 00:02:34,480 --> 00:02:38,890 For example let's say otherwise we throw an error message with throw in you. 35 00:02:38,890 --> 00:02:42,670 Error no report found. 36 00:02:42,680 --> 00:02:49,680 So now we added more logic more complex logic to this property and when we want to retrieve it we can 37 00:02:49,680 --> 00:02:55,880 directly retrieve his property through the dot notation but we can use this as a property to Deandra 38 00:02:55,950 --> 00:02:57,250 on this logic. 39 00:02:57,290 --> 00:03:04,040 So how what does work well down there where I create my accounting department before I add my report 40 00:03:04,430 --> 00:03:13,220 if I tried to console lock accounting dot most recent report and now important accounting dot most recent 41 00:03:13,220 --> 00:03:19,850 report and now important You access this as a property so no parentheses here you don't execute does 42 00:03:19,850 --> 00:03:23,020 as a method you just access it like a normal property. 43 00:03:23,090 --> 00:03:27,870 And behind the scenes this will execute this method up there. 44 00:03:27,950 --> 00:03:34,390 So if I tried this now before we added a report we'll see that this rebuilt without errors recompile 45 00:03:34,640 --> 00:03:35,440 without errors. 46 00:03:35,450 --> 00:03:38,110 But of course here an error is thrown. 47 00:03:38,150 --> 00:03:42,910 It's our custom no report found error because no report was added yet. 48 00:03:43,010 --> 00:03:50,210 And since using this as a getter accesses this method here and executes it if we have no report we don't 49 00:03:50,210 --> 00:03:56,920 make it into this check we're into this if statement in day out for this line here it kicks in as an 50 00:03:56,920 --> 00:04:04,450 alternative if we would move that down after we added a report then of course we can save that and we 51 00:04:04,450 --> 00:04:08,440 no longer get an error but instead we output this last report. 52 00:04:08,440 --> 00:04:15,070 So that's a getter and you can also add a setter a setter is added almost the same way you used the 53 00:04:15,070 --> 00:04:16,450 set keyword. 54 00:04:16,450 --> 00:04:22,900 And then again any name of your choice typically a name related to the property which should be set. 55 00:04:22,900 --> 00:04:29,370 So here I will reuse most recent report so that we can use this both to read a value and to set a value. 56 00:04:29,440 --> 00:04:31,840 And then again you defined us like a method. 57 00:04:31,840 --> 00:04:39,000 But now this needs to take an argument the value which the user would have passed in here. 58 00:04:39,060 --> 00:04:41,170 This should be a string value. 59 00:04:41,170 --> 00:04:46,180 And now in here you can run any logic you want to store this. 60 00:04:46,210 --> 00:04:52,210 Now here since we're passing in a value for the most recent report I in the end want to execute add 61 00:04:52,210 --> 00:04:53,110 report. 62 00:04:53,140 --> 00:04:57,190 So here a setter could simply be an alternative to the ADD report method. 63 00:04:57,190 --> 00:05:03,780 So in here we can access this add report this is required since we're inside of the class. 64 00:05:03,790 --> 00:05:10,360 We refer to the class and then to this class method here and now we can just forward value here. 65 00:05:10,410 --> 00:05:16,040 Now we could add a more complex logic and also check if value is maybe not the find if it's false see 66 00:05:16,300 --> 00:05:24,730 and then just return or throw a new error where we say Please pass in a valid value that is something 67 00:05:24,730 --> 00:05:26,040 we could also do. 68 00:05:26,320 --> 00:05:28,720 And now we're using a setter here as well. 69 00:05:29,050 --> 00:05:32,410 With that added We can test it down there. 70 00:05:32,530 --> 00:05:40,520 I could add most recent report I could access does on accounting and just like this we would read from 71 00:05:40,520 --> 00:05:40,720 it. 72 00:05:40,760 --> 00:05:46,450 But now by adding a equals sign we're trying to set a value to that and that will trigger this setter 73 00:05:46,460 --> 00:05:53,440 method again just as before we don't execute it as a method but simply access is like a property now 74 00:05:53,440 --> 00:06:00,670 here if I pass in an empty string then we technically do pass in a string but an empty string will be 75 00:06:00,670 --> 00:06:04,970 treated as false see here and therefore we should now get an error. 76 00:06:04,990 --> 00:06:12,460 So if I save this line of code if I save the code with this line of code added we indeed get this error 77 00:06:12,490 --> 00:06:14,590 please pass it in a valid value. 78 00:06:14,590 --> 00:06:23,830 If I do pass an invalid value though if I say year end report here for example and I now save this. 79 00:06:23,930 --> 00:06:29,630 Now you will see this all works and year end report as part of our reports list. 80 00:06:29,690 --> 00:06:35,390 So these are getters and setters which can be great for encapsulating logic and for adding extra logic 81 00:06:35,390 --> 00:06:40,100 that should run when you try to read a property or when you try to set a property.