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
And here it is, 2
2
the final coding challenge 3
3
about object oriented programming. 4
4
And in this challenge, 5
5
the goal is to recreate the challenge number 3, 6
6
but this time using ES6 classes. 7
7
So basically what you should do, 8
8
is to create a class EV, 9
9
so electric vehicle. 10
10
Which is going to be a child class, 11
11
of the car class that we already created before as well. 12
12
Then in that EV class, 13
13
make the charge property private. 14
14
So that's number two. 15
15
And then point number three, 16
16
is that I want you to implement the ability 17
17
to chain the accelerate and charged battery methods 18
18
on this class. 19
19
And the same also for the brake method in the car class. 20
20
So that's actually called CarCL. 21
21
So then go ahead and experiment with chaining, 22
22
and all of that should be done 23
23
using this test data down here. 24
24
All right. 25
25
So I already have to coat here. 26
26
And so this makes it a bit easier 27
27
to understand what the goal is. 28
28
So, as I mentioned, 29
29
we already have the CarCL class from before, 30
30
and so this one is basically going to stay the same, 31
31
except for the break method, 32
32
which I want you to make chainable. 33
33
And then here we have the EV already, 34
34
as we implemented it previously using constructor functions. 35
35
And so the big goal of this challenge, 36
36
is basically to convert all of this, 37
37
into a child class of the CarCL class. 38
38
All right, 39
39
so let's do this. 40
40
So class EVCL, 41
41
and now this should be a child class. 42
42
And so we use the extends keyword. 43
43
So this extends the CarCL class. 44
44
And now let's put this curly brace actually down here, 45
45
because all we need to do is to now modify this 46
46
a little bit. 47
47
So here we can transform this here 48
48
into the constructor. 49
49
So the constructor method, 50
50
and we still need to make speed and charge Of course. 51
51
Now here we don't need to call the car, 52
52
but instead we call super. 53
53
Remember, and we do that of course, 54
54
without the disc keyword, 55
55
because all of this 56
56
will automatically happen behind the scenes. 57
57
Okay. So if I save this now, 58
58
we get to this error, 59
59
but that's because 60
60
we still have a lot of stuff to change down here. 61
61
Next here is linking off the prototypes. 62
62
And none of this is necessary with classes, 63
63
because all of this simply happens automatically 64
64
behind the scenes. 65
65
We also don't need to set anything manually 66
66
on the prototype properties. 67
67
So let's get rid of this. 68
68
And in here also, 69
69
we don't need to function keyword. 70
70
And so with this, 71
71
uh, I think for now we should be good to go. 72
72
So let's now create an object called Rivian, 73
73
which is going at 120 with a charge of 23%. 74
74
So Rivian, 75
75
which is, uh, another electric vehicle brand. 76
76
So EVCL Rivian 120 and 23% of charge. 77
77
Let's check it out here, 78
78
and yup, there it is. 79
79
Great. 80
80
So next up the task, 81
81
is to make the charge private. 82
82
So let's do that. 83
83
So this is the CarCL. 84
84
So here we don't have to charge in the parent class, 85
85
but we do have it here in the child class. 86
86
Now we receive this charge, 87
87
actually as an input here. 88
88
And so we still need to define 89
89
all the fields outside of any method. 90
90
So we do that up here, 91
91
and that is true for both private and public fields. 92
92
So in this case, 93
93
we want a private field, 94
94
and that's called charge, 95
95
and then we don't give it any value. 96
96
And here we simply reassign that. 97
97
And then all we have to do is to change it everywhere. 98
98
So that's here and here and here, 99
99
give it a save. 100
100
If we now check this, 101
101
then indeed here it is. 102
102
And we didn't get any error, 103
103
but indeed, as we try to now access this property outside, 104
104
then that is not going to work. 105
105
So it is now truly private, 106
106
and it is encapsulated inside of the class. 107
107
So from outside, 108
108
there is no way of changing it, 109
109
except of course by charging the battery or by accelerating. 110
110
And so once again, 111
111
this method here is basically a public API, 112
112
so that we can manipulate 113
113
the charge property from the outside. 114
114
But without being able to directly access the property 115
115
from the outside. 116
116
So next up, 117
117
we want to implement a chaining 118
118
on accelerate, chargeBattery and brake. 119
119
So on brake, 120
120
we need to do it here. 121
121
And so remember, 122
122
all we have to do is to return this keyword, right? 123
123
So that's very easy, 124
124
here to same, 125
125
this. 126
126
And again, we are doing this here, 127
127
basically on all the methods that actually set something. 128
128
And so that's why this makes the most sense. 129
129
Great. So now we are able to do something like this. 130
130
So we can accelerate, 131
131
and then we can accelerate again, 132
132
and we can accelerate again maybe. 133
133
Let's brake for a change, 134
134
then let's charge the battery, 135
135
let's say to 50%, 136
136
and then let's accelerate some more. 137
137
And so this is what the result is going to be looking like. 138
138
So here we are accelerating three times, 139
139
then we are breaking down by five kilometers per hour, 140
140
and then immediately, 141
141
we add 20 kilometers per hour back again. 142
142
And the charge then drops to 49. 143
143
And that's because previously we had charged it to 50. 144
144
And what's also interesting to see here, 145
145
is that, or child class does also inherit 146
146
the getters and setters from the parent class. 147
147
So we can also do this. 148
148
So remember, 149
149
that is a setter and a getter that we defined previously. 150
150
And so this is basically 195 kilometers per hour, 151
151
converted to miles per hour. 152
152
Okay. And with that, 153
153
this challenge is now completed. 154
154
And I think we used all the things 155
155
that we learned over the course of this section 156
156
right here in all of these four challenges. 157
157
So this should cover everything that you learned. 158
158
And so by now, 159
159
you actually already put everything 160
160
that we learned about object oriented programming 161
161
into practice by yourself. 162
162
So congratulations for that. 163
163
That was a really great job, 164
164
and a great effort 165
165
to watch this entire section until the end. 166
166
And now it's time to make this even more exciting 167
167
and to do that, 168
168
we will create an entire brand new project, 169
169
which will be completely based on objects 170
170
in the next section. 171
171
So that project is going to be 172
172
one of the most exciting things 173
173
of this course, in my opinion. 174
174
So I hope to see you there soon, 175
175
once you're finished, 176
176
basically digesting this section. 177
177
So congratulations once again. 178
178
I'm really happy to still have you with me, 179
179
here at this point of the course. 180
180
And so I see you back here in the next section.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.