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
There are a few more things that we need 2
2
to learn about classes. 3
3
And so, let's actually create a new class now. 4
4
And as an example, 5
5
we are gonna use the bank account 6
6
that we implemented before, in the Bankist app. 7
7
Remember that? 8
8
So, let's say class Account, and then as always, 9
9
we need the constructor method. 10
10
And here each account should have the owner name. 11
11
It should have a defined currency, 12
12
and also, we want to pass in a pin. 13
13
And so, let's say this.owner is owner 14
14
and the same for currency and the pin. 15
15
So, we already know how to do all of this, right? 16
16
And so now let's create a new account here. 17
17
Let's say acc1, and so new account, 18
18
and let's say that I am the owner. 19
19
So, Jonas, then the currency is in Euro and the pin, 20
20
let's say, is this one? 21
21
All right. 22
22
So, indeed we have the three values that we passed 23
23
into the constructor, now inside of the object. 24
24
But now what about the movements array, 25
25
and maybe also the local. 26
26
So, we want to start always with an empty array 27
27
as the movements and the local, 28
28
we want to get from the navigator.language. 29
29
So, how should we do that? 30
30
Should we add a new parameter, like this? 31
31
So, movement, and then always pass in an empty array 32
32
in all the accounts that we create. 33
33
Well, that would work. 34
34
So, let's actually see that. 35
35
So, movement, actually movements should be equal 36
36
to movements, here as well. 37
37
And so now, as you see, 38
38
the movement is now this empty array, 39
39
however, it doesn't make much sense 40
40
to pass in this empty array, 41
41
into all the new accounts that we want to create, right? 42
42
So, we don't need this, and instead we can simply do this. 43
43
And so, this is something that we didn't do before, 44
44
but of course we can create even more properties 45
45
on any instance and properties 46
46
that are not based on any inputs. 47
47
And the same we can now do for the local 48
48
and set it to navigator.language, right? 49
49
And so now, all of that appears here as well. 50
50
And in fact, we can even execute any code 51
51
here in this constructor that we want. 52
52
So, let's say, "Thanks for opening an account," 53
53
and then owner. 54
54
And so, when Jonas opens a new account, 55
55
then he's basically greeted with this message here, 56
56
coming right from the constructor. 57
57
All right, but now what about the deposits and withdrawals? 58
58
So, basically what about our movements? 59
59
Well, we could simply now go ahead and do this. 60
60
So we can take acc1.movements, 61
61
and then if we wanted to do a deposit, 62
62
we could push a value into this. 63
63
Let's say 250, and then when we want to withdraw something, 64
64
we would simply push a negative value. 65
65
And so, if we then take a look at our account here, 66
66
then of course we would see these movements right here 67
67
in our array. 68
68
However, it's not a good idea at all to do this. 69
69
So, instead of interacting with a property like this, 70
70
it's a lot better to create methods 71
71
that interact with these properties. 72
72
And that is especially true for important properties, 73
73
such as these movements here. 74
74
So, this will for sure avoid bugs 75
75
in the future, as your application grows. 76
76
So, let's now actually create a deposit 77
77
and a withdrawal method here. 78
78
So, deposit and inherit the value that we want to deposit. 79
79
And then this.movements.push, 80
80
the value that we pass into. 81
81
And now the same for withdrawal. 82
82
So, also a value. 83
83
And so here, we can now actually call this method 84
84
because it's actually gonna work basically the same way. 85
85
So this.deposit. 86
86
And so, as you can see here, 87
87
we can actually call other methods 88
88
inside of a certain method. 89
89
So, in withdraw, we are calling this.deposit, 90
90
but of course we still need to use the this keyword 91
91
to be able to access this other method. 92
92
And then here, we pass in minus the value 93
93
that we received in this function. 94
94
So let's try this now. 95
95
So instead of doing this, 96
96
we now can do it in a much nicer way. 97
97
So acc1.deposit 250, and then acc1.withdraw 140. 98
98
And so indeed, 99
99
our movements array looks just the same as before, 100
100
but now we are actually using this public interface 101
101
that we built here, right? 102
102
So, basically these methods here, 103
103
are the interface to our objects, 104
104
and we also call this API, remember? 105
105
So, we talked about this, 106
106
right at the beginning of the section. 107
107
So again, let's actually write this here. 108
108
So this is the public interface of our objects. 109
109
And so, yeah, this is a lot better 110
110
than having to manually manipulate 111
111
these properties outside of the object, as we did here. 112
112
Also this withdraw method here actually abstracts the fact 113
113
that a withdrawal is basically a negative movement. 114
114
So here as we did it manually, 115
115
we needed to pass in minus 140. 116
116
But now, as we do a withdrawal, it's of course, 117
117
a lot more natural to write 118
118
that simply 140 are gonna be withdrawn. 119
119
So, what I'm trying to say is that this minus here, 120
120
is something that the user of this object, 121
121
shouldn't be caring about. 122
122
And so now, we actually abstracted that away, 123
123
right into this object, 124
124
and in particular into this withdrawal method. 125
125
So, just a very small obstruction here in this case, 126
126
but it's still worth to notice. 127
127
All right, now, of course, 128
128
still there is nothing stopping someone on our team 129
129
from interacting with the movements array directly 130
130
and potentially making mistakes and introducing bugs. 131
131
So, simply the fact that we have these methods, 132
132
doesn't make it impossible to still do this. 133
133
And the same goes, for example, for the pin. 134
134
So, of course, we can access the pin 135
135
from outside of the account. 136
136
So, you see, but probably it shouldn't be accessible 137
137
from outside of the class, but yeah, 138
138
of course, right now it is accessible. 139
139
And this is actually a very real and very important concern. 140
140
So, it's not just something theoretical 141
141
that I'm telling you here. 142
142
And the same, of course, goes for methods. 143
143
So, let's say we have a requestLoan method here, 144
144
for some value. 145
145
And remember that this was also a functionality 146
146
of the Bankist application. 147
147
So, we could make the approval of the loan based 148
148
on some condition, and that condition could come 149
149
from some other method, let's say, 150
150
approveLoan, let's pass in a value here as well, 151
151
and this one will simply always return true. 152
152
So, I don't want to implement any complex logic here now, 153
153
but anyway, here we can now call this.approveLoan 154
154
with the value. 155
155
And so if the loan is approved, 156
156
then this.deposit of the value, 157
157
and we can lock something to the console. 158
158
So, "Loan approved" or something like that, okay? 159
159
Now, in the public interface, 160
160
we basically only want this method here. 161
161
So, we want to be able to do this here basically, 162
162
acc1.requestLoan, let's say of 1000. 163
163
And so, we see that the loan was approved 164
164
and it was pushed into our array. 165
165
But, of course, we are also able to do this. 166
166
So approveLoan. 167
167
And so this, of course, doesn't do anything, 168
168
but in the real world, 169
169
we should not even be allowed to access this kind of method. 170
170
So this is kind of an internal method 171
171
that only the requestLoan method should be able to use. 172
172
And so the reason why I'm telling you all this, 173
173
is basically just to justify 174
174
that we really need data encapsulation and data privacy. 175
175
And we talked about these topics in the introductory lecture 176
176
of the section, and so as always, 177
177
you can go ahead and review that, 178
178
but anyway, in the next video, 179
179
we will finally start implementing data privacy 180
180
and data encapsulation. 181
181
So let's move on there right now.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.