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
Welcome back to your first coding challenge 2
2
of this section. 3
3
So now it's time to finally put everything that you learned 4
4
in the section so far into practice. 5
5
So in this section, 6
6
I want you to use a Constructor Function 7
7
in order to implement a very simple car all right? 8
8
So for this purpose, a car has just a make 9
9
and a speed property, 10
10
and the speed is simply the current speed of the car 11
11
measured in kilometers per hour, 12
12
then I want you to implement an accelerate method 13
13
that will increase the car speed by 10, 14
14
and it would also log the new speed to the console, 15
15
then another method called brake, 16
16
which will then decrease the speed 17
17
but only by five all right? 18
18
And finally I want you to use this test data down here 19
19
to implement these two cars. 20
20
So basically to create two car objects 21
21
and then experiment with calling the two methods 22
22
that we just created on each of the objects. 23
23
And of course you should use all the knowledge 24
24
from the previous lectures to doing this. 25
25
So I hope that this is a fun challenge 26
26
that's not too hard. 27
27
So take all the time that you need 28
28
and I see you back here when you're ready with my solution. 29
29
All right so hopefully you did that successfully, 30
30
so let's get started here, 31
31
so we create the Constructor Function for car 32
32
and one more time with a capital C 33
33
and then we want each car object to have a make and a speed 34
34
and so that's the arguments 35
35
that we're gonna pass into this function. 36
36
And then just as before under this property, 37
37
we will set the make to the make that we receive 38
38
and the speed, 39
39
to the speed that we receive as well. 40
40
And so this is basically always gonna work the same way. 41
41
So as I mentioned, 42
42
this is a pattern that developers came up with 43
43
and so now we just simply follow this 44
44
like a recipe all right? 45
45
And with this, we can actually already create our two cars, 46
46
so let's say the BMW is a new car 47
47
and so remember this new operator here 48
48
is what's gonna enable all of this to work. 49
49
And so here the make is of course BMW 50
50
and the speed is 120 kilometers per hour, 51
51
which for my American friends is 75 miles per hour 52
52
I believe 53
53
so the next one is gonna be the Mercedes. 54
54
So that's new car 55
55
and at speed, 95 kilometers per hour okay? 56
56
So let's just take a look 57
57
and so yeah, 58
58
that appears to be working just fine. 59
59
And so now let's implement our two methods here, 60
60
so accelerate and brake 61
61
and of course we are not going to add the method right here. 62
62
So we will not do this .accelerate 63
63
because as we just learned before, 64
64
that would be terrible for performance, 65
65
especially if we had many, many car objects. 66
66
So instead we will of course make use 67
67
of prototypal inheritance. 68
68
So we take the prototype property of car 69
69
and to that, we add the accelerate property accelerate 70
70
that's correct 71
71
and we make it a method by passing in a function 72
72
or actually by specifying a function here. 73
73
So once again, 74
74
we can use the This keyword in here 75
75
because that is simply gonna be the object 76
76
on which the method will be called 77
77
and so that object will have the speed property 78
78
and so here we can then increase that by 10. 79
79
And then we can also log that to the console 80
80
and we can even make use of course of the make. 81
81
So this one wasn't specified in the text, 82
82
but we can log a nice string, like this 83
83
is going 84
84
at this.speed 85
85
and then kilometers per hour 86
86
but again this part, 87
87
you wouldn't have to implement like this. 88
88
And now let's just take this here 89
89
for the brake because it's gonna be pretty similar, 90
90
so brake, 91
91
and all we do then is to 92
92
decrease the speed by five all right? 93
93
And now let's just call this on the BMW a couple of times, 94
94
so bmw.accelerate, 95
95
and let's do that three times. 96
96
And so after three times, 97
97
the BMW is going at 150 kilometers per hour, 98
98
so apparently that works just fine. 99
99
Let's just add a brake here in the middle 100
100
and so now we reduce the speed a little bit 101
101
and then back up to 145 now okay? 102
102
And that's actually it, 103
103
that's all we had to do in this challenge, 104
104
and maybe with this example, 105
105
you can start to see the advantages 106
106
of object oriented programming in action. 107
107
So all the advantages that we talked about 108
108
right in the first lecture of the section, 109
109
and essentially the part where we learned 110
110
that in object oriented programming, 111
111
we pack both the functionality and the data into objects. 112
112
And so that's exactly what we have here, 113
113
so each of these objects, 114
114
so BMW and Mercedes contains the state of the car, 115
115
so the current speed and the make, 116
116
and it also contains the functionality 117
117
to manipulate its own data, 118
118
so the accelerate and brake methods in this case 119
119
are able to manipulate the speed, right? 120
120
And at the same time, 121
121
these two methods also form the public interface 122
122
of this object, right? 123
123
So right now the rest of our code 124
124
can interact with these objects 125
125
by calling the brake and the accelerate methods. 126
126
Now in this case, of course, 127
127
this is just a very small and isolated example, 128
128
so it can still be hard to see the advantages of this, 129
129
but maybe you can take a minute or two 130
130
and compare this style of coding 131
131
with the code that we have written up until this point 132
132
in the course. 133
133
All right, so at some point you will see 134
134
that this is really a completely different way of thinking 135
135
about our code, 136
136
but anyway that's it for this challenge. 137
137
In the next video, 138
138
we are gonna talk about ESX classes, 139
139
so I hope to see you there soon.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.