1
00:00:01,380 --> 00:00:04,530
<v Jonas>So as we reach the end of this section,</v>

2
00:00:04,530 --> 00:00:08,640
let's quickly review all the terminology around classes

3
00:00:08,640 --> 00:00:11,650
because there is so much different stuff going on.

4
00:00:11,650 --> 00:00:15,270
So a lot of different features that you can use.

5
00:00:15,270 --> 00:00:17,800
And since classes are probably the way

6
00:00:17,800 --> 00:00:21,230
you're gonna implement OOP by yourself,

7
00:00:21,230 --> 00:00:25,200
it's always good to have a nice overview of everything

8
00:00:25,200 --> 00:00:26,453
that we can do with them.

9
00:00:28,510 --> 00:00:30,950
So here we have a huge class,

10
00:00:30,950 --> 00:00:34,060
which essentially contains all the features

11
00:00:34,060 --> 00:00:37,740
that we studied over the last couple of lectures.

12
00:00:37,740 --> 00:00:41,370
So this is how we define a class.

13
00:00:41,370 --> 00:00:44,810
And in this case, it is actually a child class.

14
00:00:44,810 --> 00:00:49,810
So student is a child class of the parent class person

15
00:00:49,860 --> 00:00:53,550
and that is because here we are using the extends keyboard

16
00:00:53,550 --> 00:00:57,730
to set up the inheritance between these two classes.

17
00:00:57,730 --> 00:01:00,130
And remember that the extends keyword

18
00:01:00,130 --> 00:01:04,560
will also automatically set up the prototype chain for us.

19
00:01:04,560 --> 00:01:06,803
So we don't have to do anything manually.

20
00:01:07,670 --> 00:01:11,570
Next up, this is how we define a public field.

21
00:01:11,570 --> 00:01:14,920
So this is what we did in a very recent lecture.

22
00:01:14,920 --> 00:01:19,080
And so remember that a public field is very similar

23
00:01:19,080 --> 00:01:22,980
to just a property that we defined in a constructor.

24
00:01:22,980 --> 00:01:26,550
So it is available on every created object.

25
00:01:26,550 --> 00:01:29,793
So on every instance created by this class.

26
00:01:30,730 --> 00:01:33,750
Then of course, we also have private fields

27
00:01:33,750 --> 00:01:36,920
and they are almost the same as public fields,

28
00:01:36,920 --> 00:01:40,980
but they are not accessible outside of the class.

29
00:01:40,980 --> 00:01:42,920
And so therefore this is perfect

30
00:01:42,920 --> 00:01:47,080
for implementing data privacy and encapsulation.

31
00:01:47,080 --> 00:01:49,940
We also have static public fields.

32
00:01:49,940 --> 00:01:53,180
And so these are fields or like properties

33
00:01:53,180 --> 00:01:56,510
that are available only on the class.

34
00:01:56,510 --> 00:01:58,690
And I didn't show them to you in the code.

35
00:01:58,690 --> 00:02:00,830
So in one of the lectures,

36
00:02:00,830 --> 00:02:03,120
but this is how we can implement them.

37
00:02:03,120 --> 00:02:05,570
So just like static methods,

38
00:02:05,570 --> 00:02:10,223
we use the static keyword to make any field static as well.

39
00:02:11,160 --> 00:02:14,180
Then here, this is the constructor method

40
00:02:14,180 --> 00:02:17,400
and it is automatically called by the new operator

41
00:02:17,400 --> 00:02:20,790
whenever we create a new instance of the class.

42
00:02:20,790 --> 00:02:23,440
So basically a new object.

43
00:02:23,440 --> 00:02:26,380
And this constructor method is mandatory

44
00:02:26,380 --> 00:02:29,800
in any regular class, but it might be omitted

45
00:02:29,800 --> 00:02:33,870
in a child class if we want it to have the exact same number

46
00:02:33,870 --> 00:02:36,873
and the exact same name of parameters.

47
00:02:38,610 --> 00:02:42,390
Then inside of the constructor there is the call

48
00:02:42,390 --> 00:02:47,180
to the parent class and so that is the superclass.

49
00:02:47,180 --> 00:02:49,860
And this of course is only necessary

50
00:02:49,860 --> 00:02:52,920
whenever we are writing a child class.

51
00:02:52,920 --> 00:02:56,190
So when we're using the extent keyword.

52
00:02:56,190 --> 00:02:58,500
And remember that this one needs to happen

53
00:02:58,500 --> 00:03:02,023
before we access the disc keyword in the constructor.

54
00:03:03,790 --> 00:03:06,800
Then here we have an instance property.

55
00:03:06,800 --> 00:03:09,430
And so just like public fields,

56
00:03:09,430 --> 00:03:13,900
the property is also available on each created object.

57
00:03:13,900 --> 00:03:16,830
But the difference between this one and the public field

58
00:03:16,830 --> 00:03:19,610
is that we set these instance properties

59
00:03:19,610 --> 00:03:23,200
based on input data of the constructor.

60
00:03:23,200 --> 00:03:26,380
So basically these properties are more personalized

61
00:03:26,380 --> 00:03:30,410
and unique for each object while the fields are usually

62
00:03:30,410 --> 00:03:33,620
for something that is common to all the objects.

63
00:03:33,620 --> 00:03:37,230
For example, here at the university for all of the students

64
00:03:37,230 --> 00:03:39,540
is the University of Lisbon.

65
00:03:39,540 --> 00:03:43,653
And so that is not unique to each object, okay.

66
00:03:44,720 --> 00:03:48,750
Then here we are redefining a private field.

67
00:03:48,750 --> 00:03:51,790
And so this is what we did with the pin

68
00:03:51,790 --> 00:03:54,930
in the bank account example, right?

69
00:03:54,930 --> 00:03:59,780
So this private field should be unique for each student.

70
00:03:59,780 --> 00:04:02,760
And so we created the private fields out there

71
00:04:02,760 --> 00:04:04,180
without any value.

72
00:04:04,180 --> 00:04:07,760
And then here we are simply redefining it to the value

73
00:04:07,760 --> 00:04:09,833
that is coming into the constructor.

74
00:04:11,400 --> 00:04:15,313
Then here, as we already know, is a normal public method.

75
00:04:16,640 --> 00:04:19,680
Here, we are referencing a private field

76
00:04:19,680 --> 00:04:22,280
and also a private method.

77
00:04:22,280 --> 00:04:24,703
And so about the private methods,

78
00:04:24,703 --> 00:04:27,120
this is how they look like.

79
00:04:27,120 --> 00:04:28,440
But as I mentioned,

80
00:04:28,440 --> 00:04:31,800
they will probably not yet work in your browser.

81
00:04:31,800 --> 00:04:33,940
At least if you're watching this close

82
00:04:33,940 --> 00:04:36,520
to the recording date of this course.

83
00:04:36,520 --> 00:04:40,030
So as an alternative, you can fake private methods

84
00:04:40,030 --> 00:04:43,873
by using the underscore convention instead of the hash.

85
00:04:45,150 --> 00:04:49,270
Next up, this is what a getter method looks like.

86
00:04:49,270 --> 00:04:52,400
And remember that a getter method is basically

87
00:04:52,400 --> 00:04:55,540
so that we can get a value out of an object

88
00:04:55,540 --> 00:04:59,800
by simply writing a property instead of writing a method.

89
00:04:59,800 --> 00:05:02,860
So in this case, we could simply write student.testscore

90
00:05:04,094 --> 00:05:07,520
and that would then run this getter method

91
00:05:07,520 --> 00:05:10,480
and the same for the setter method.

92
00:05:10,480 --> 00:05:13,740
So in this case, we can simply define the test score

93
00:05:13,740 --> 00:05:15,910
by setting it to some value

94
00:05:15,910 --> 00:05:19,450
instead of calling a test score method.

95
00:05:19,450 --> 00:05:22,300
And keep in mind that if you have a setter

96
00:05:22,300 --> 00:05:26,500
for a property that is already defined in the constructor,

97
00:05:26,500 --> 00:05:29,830
then you need to create basically a new property

98
00:05:29,830 --> 00:05:32,530
with the underscore in front of it.

99
00:05:32,530 --> 00:05:35,570
So again, that is kind of a convention

100
00:05:35,570 --> 00:05:37,720
that you should use in this case.

101
00:05:37,720 --> 00:05:40,320
And then in the getter with the same name,

102
00:05:40,320 --> 00:05:44,190
you also need to then return that new property.

103
00:05:44,190 --> 00:05:46,580
So we did that in one of the lectures,

104
00:05:46,580 --> 00:05:49,690
when we did a name validation for the name

105
00:05:49,690 --> 00:05:52,270
that was passed into the constructor.

106
00:05:52,270 --> 00:05:53,103
Remember that?

107
00:05:54,940 --> 00:05:58,480
Next, this is how your write static methods

108
00:05:58,480 --> 00:06:02,700
and a static method is available only on the class.

109
00:06:02,700 --> 00:06:07,230
So it cannot access the instance properties nor the methods,

110
00:06:07,230 --> 00:06:08,873
but only the static ones.

111
00:06:09,710 --> 00:06:12,460
So for example, that static public fields

112
00:06:12,460 --> 00:06:14,530
that we defined there in the top

113
00:06:14,530 --> 00:06:18,190
will of course be accessible in the static method.

114
00:06:18,190 --> 00:06:20,950
And usually we use these static methods

115
00:06:20,950 --> 00:06:23,423
as helper methods for the class.

116
00:06:24,670 --> 00:06:28,450
Finally, this is how you then create a new object

117
00:06:28,450 --> 00:06:30,650
using the new operator.

118
00:06:30,650 --> 00:06:33,893
And so this should be nothing new at this point for you.

119
00:06:35,370 --> 00:06:37,580
Now, here's just a couple of things

120
00:06:37,580 --> 00:06:40,020
that we need to remember about classes

121
00:06:40,020 --> 00:06:43,550
and that I actually already mentioned before as well.

122
00:06:43,550 --> 00:06:46,750
So keep in mind that classes are really just

123
00:06:46,750 --> 00:06:50,250
syntactic sugar over constructor functions.

124
00:06:50,250 --> 00:06:55,250
Also classes are not hoisted, they are first class citizens

125
00:06:55,300 --> 00:06:59,930
and the class body is always executed in strict mode.

126
00:06:59,930 --> 00:07:02,870
Okay, and there you'll have it.

127
00:07:02,870 --> 00:07:06,110
This is an overview and also a summary

128
00:07:06,110 --> 00:07:09,593
of the entire syntax of classes in JavaScript.

