All language subtitles for 22. Chaining Methods

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.