Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
In the last lecture we implemented a new class 2
2
which showed us the need for encapsulation and data privacy. 3
3
So let's now tackle this very important principle 4
4
of object oriented programming. 5
5
Now first, remember that encapsulation basically means 6
6
to keep some properties and methods private inside the class 7
7
so that they are not accessible from outside of the class. 8
8
Then the rest of the methods are basically exposed 9
9
as a public interface, which we can also call API. 10
10
So this is essential to do in anything 11
11
more than a toy application. 12
12
Now, there are two big reasons why we need encapsulation 13
13
and data privacy. 14
14
So first it is to prevent code 15
15
from outside of a class to accidentally manipulate 16
16
or data inside the class. 17
17
And that's exactly what we did 18
18
by the end of the last lecture here. 19
19
So this is also the reason why 20
20
we implement a public interface. 21
21
So we are not supposed to manually mess with this property 22
22
and therefore we should encapsulate it. 23
23
Now, the second reason is 24
24
that when we expose only a small interface 25
25
so a small API consisting only of a few public methods 26
26
then we can change all the other internal methods 27
27
with more confidence. 28
28
Because in this case, we can be sure 29
29
that external code does not rely on these private methods. 30
30
And so therefore our code will not break 31
31
when we do internal changes. 32
32
So that's what encapsulation 33
33
and data privacy are and the reasons for it. 34
34
And so let's now actually implement it. 35
35
However JavaScript classes actually 36
36
do not yet support real data privacy and encapsulation. 37
37
Now there is a proposal to add truly private class fields 38
38
and methods to the language, 39
39
but it's not completely ready yet. 40
40
I will still show it to you in the next lecture 41
41
because it's really cool 42
42
but even when this feature ships in the browser 43
43
it will take some time until you can use it with confidence. 44
44
And so in this lecture, we will basically fake encapsulation 45
45
by simply using a convention. 46
46
So the first candidate to protect here is again, 47
47
this movements array that we have been talking about. 48
48
So the movements are mission critical data 49
49
and so here we will protect this data 50
50
so that no one can accidentally manipulate it. 51
51
And for now, all we will do is to add this underscore 52
52
in front of the property name and that's it. 53
53
Now we need to then go ahead and change it everywhere. 54
54
So that's here and yeah, that's actually it. 55
55
So again this does not actually make 56
56
the property truly private 57
57
because this is just a convention. 58
58
So it's something that developers agree to use 59
59
and then everyone does it this way. 60
60
But since it is not truly private we call this protected, 61
61
so protected property. 62
62
And so now if we wanted to get the movements outside here 63
63
we could, of course still do this. 64
64
So that's what I was just saying 65
65
which is that the data here is of course still accessible 66
66
if we use this underscore outside here as well 67
67
but at least now everyone on your team 68
68
and that includes yourself will know that this property 69
69
is not supposed to be touched outside of the class. 70
70
So you can still do it 71
71
but at least you will know that it is wrong, okay? 72
72
Now, if we still wanted to give access 73
73
to the movements array from the outside 74
74
then we would have to implement a public method for that. 75
75
So let's do that actually right here. 76
76
So let's say get movements. 77
77
And of course we could also create a getter here 78
78
instead of this method, but let's just keep it simple. 79
79
So it's very common to actually have a method 80
80
that is called get or set instead of using a real getter 81
81
or a real setter. 82
82
And so all that this will do 83
83
is to return this.movements. 84
84
And so now this would be 85
85
the correct way to get the movements, so get movements. 86
86
And so this one everyone 87
87
can still at least access the movements 88
88
but they cannot override them. 89
89
So they cannot set the movements 90
90
unless of course they use the underscore with the convention 91
91
but then at least they will know 92
92
that it's wrong to access the property. 93
93
Next, we could also protect here the pin. 94
94
So this is also something that doesn't make much sense 95
95
to be accessible from the outside. 96
96
And in fact, we could make everything protected here 97
97
but I think that like this, we are good to go. 98
98
So this is just a small example anyway. 99
99
And so for now, let's leave it like this. 100
100
To finish, let's just also protect 101
101
then the approved loan method. 102
102
So this one I also mentioned by the end 103
103
of the previous lecture. 104
104
So this is a method that is only 105
105
supposed to be internally by the bank. 106
106
Let's say basically just to check 107
107
if a loan should be approved. 108
108
So essentially this method here should not be part 109
109
of the public API, but all the others should be. 110
110
So right now we are protecting this method. 111
111
And so our public API right now consists 112
112
of these other four remaining methods, all right? 113
113
So this is how we protect fields from unwanted access. 114
114
Now, as I said, of course, developers need to know 115
115
about this convention and need to follow it 116
116
because otherwise everything will still be public. 117
117
But now in the next lecture, we're actually gonna talk 118
118
about truly private fields and methods. 119
119
And so with that, we will then fix this problem for good.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.