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
Do you remember how we chained array methods 2
2
one after another, for example filter map and reduce? 3
3
So by chaining these methods, 4
4
we could first filter an array, 5
5
then map the result. 6
6
And finally reduce the results of the map, 7
7
all in one line of code. 8
8
And we can actually implement the same ability 9
9
of chaining methods in the methods of our class. 10
10
And so let's go do that now. 11
11
And actually, this is extremely easy to do. 12
12
So all we have to do is to return the object itself 13
13
at the end of a method that we want to be chainable. 14
14
So let's say that we wanted to do this. 15
15
So let me take that away from here. 16
16
So chaining. 17
17
So let's say we wanted to do account, 18
18
and then deposit, 19
19
let's say, 300. 20
20
And then right afterwards, we wanted 21
21
to deposit again on the same account. 22
22
So we would like to call deposit again, right away, 23
23
let's say 500 this time. 24
24
And then immediately after that, 25
25
let's say we want to withdraw 35. 26
26
Maybe we also want to request a loan in the middle of this. 27
27
So request loan, let's say 25,000. 28
28
And then finally, we want to withdraw some more, 29
29
let's say 4000. 30
30
So right now, this is not gonna work. 31
31
So let's see the error. 32
32
And so the problem here is that... 33
33
this one here will work. 34
34
But this will then return nothing. 35
35
Because let's see, 36
36
the deposit method does return undefined 37
37
because we're not returning anything explicitly here. 38
38
And so then undefined gets returned. 39
39
And so then here, in this next one 40
40
we are trying to call the deposit method on undefined 41
41
which is the result of all this. 42
42
And therefore we get exactly that error here. 43
43
So what we need to do is to call deposit 44
44
actually on an account. 45
45
And so what we want is for the result of this year 46
46
to be again the account, 47
47
right? 48
48
And so let's do that. 49
49
So all we have to do is return... 50
50
this. 51
51
Because this is of course, the current object. 52
52
And now we do the same on all of them. 53
53
Okay. 54
54
And here the same. 55
55
And that's it. 56
56
So returning this 57
57
will essentially make the method chainable. 58
58
And this makes most sense, in methods 59
59
that actually set some property. 60
60
And so all of the three, that we just did this, 61
61
actually do that. 62
62
So this one sets the movements here. 63
63
This one, actually two, 64
64
and this one also changes the movement array. 65
65
And so all of these, basically set some property. 66
66
And so these are then very useful to make chainable. 67
67
So let's see now. 68
68
And... 69
69
Well, we get no error, 70
70
and we get loan approved. 71
71
So that means that probably everything worked here. 72
72
So let's take a look at our account here. 73
73
Actually, let's call account getMovements. 74
74
So that's why we have this method in the public API. 75
75
And so indeed, all the deposits and withdrawals 76
76
that we just did, are now in this Movements array. 77
77
Great. 78
78
So with this, you have yet another tool in your toolbox now. 79
79
So experiment a little bit with this on your own. 80
80
And maybe also when you start writing your own classes. 81
81
All right. 82
82
So with this one 83
83
I actually showed you all there is 84
84
to show about Object Oriented Programming in JavaScript. 85
85
So what a long section here, but also a beautiful one, 86
86
if you ask me. 87
87
Now, just to really wrap up this section. 88
88
Next up, I have a nice overview of the entire class syntax 89
89
and then one final coding challenge.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.