All language subtitles for 22. Chaining Methods

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 1 00:00:01,080 --> 00:00:03,980 Do you remember how we chained array methods 2 2 00:00:03,980 --> 00:00:08,980 one after another, for example filter map and reduce? 3 3 00:00:09,230 --> 00:00:11,440 So by chaining these methods, 4 4 00:00:11,440 --> 00:00:13,720 we could first filter an array, 5 5 00:00:13,720 --> 00:00:15,400 then map the result. 6 6 00:00:15,400 --> 00:00:18,400 And finally reduce the results of the map, 7 7 00:00:18,400 --> 00:00:20,860 all in one line of code. 8 8 00:00:20,860 --> 00:00:23,910 And we can actually implement the same ability 9 9 00:00:23,910 --> 00:00:28,080 of chaining methods in the methods of our class. 10 10 00:00:28,080 --> 00:00:30,163 And so let's go do that now. 11 11 00:00:31,620 --> 00:00:35,320 And actually, this is extremely easy to do. 12 12 00:00:35,320 --> 00:00:39,170 So all we have to do is to return the object itself 13 13 00:00:39,170 --> 00:00:42,043 at the end of a method that we want to be chainable. 14 14 00:00:42,920 --> 00:00:46,363 So let's say that we wanted to do this. 15 15 00:00:47,730 --> 00:00:50,143 So let me take that away from here. 16 16 00:00:52,130 --> 00:00:53,223 So chaining. 17 17 00:00:55,210 --> 00:00:58,423 So let's say we wanted to do account, 18 18 00:00:59,650 --> 00:01:01,520 and then deposit, 19 19 00:01:01,520 --> 00:01:03,780 let's say, 300. 20 20 00:01:03,780 --> 00:01:06,200 And then right afterwards, we wanted 21 21 00:01:06,200 --> 00:01:08,513 to deposit again on the same account. 22 22 00:01:09,390 --> 00:01:13,570 So we would like to call deposit again, right away, 23 23 00:01:13,570 --> 00:01:16,000 let's say 500 this time. 24 24 00:01:16,000 --> 00:01:18,660 And then immediately after that, 25 25 00:01:18,660 --> 00:01:21,203 let's say we want to withdraw 35. 26 26 00:01:22,610 --> 00:01:26,830 Maybe we also want to request a loan in the middle of this. 27 27 00:01:26,830 --> 00:01:30,973 So request loan, let's say 25,000. 28 28 00:01:31,810 --> 00:01:34,983 And then finally, we want to withdraw some more, 29 29 00:01:36,010 --> 00:01:37,223 let's say 4000. 30 30 00:01:38,200 --> 00:01:40,593 So right now, this is not gonna work. 31 31 00:01:41,570 --> 00:01:43,480 So let's see the error. 32 32 00:01:43,480 --> 00:01:46,830 And so the problem here is that... 33 33 00:01:46,830 --> 00:01:49,020 this one here will work. 34 34 00:01:49,020 --> 00:01:51,870 But this will then return nothing. 35 35 00:01:51,870 --> 00:01:53,453 Because let's see, 36 36 00:01:54,450 --> 00:01:57,830 the deposit method does return undefined 37 37 00:01:57,830 --> 00:02:01,133 because we're not returning anything explicitly here. 38 38 00:02:02,730 --> 00:02:05,610 And so then undefined gets returned. 39 39 00:02:05,610 --> 00:02:07,740 And so then here, in this next one 40 40 00:02:07,740 --> 00:02:11,860 we are trying to call the deposit method on undefined 41 41 00:02:11,860 --> 00:02:14,110 which is the result of all this. 42 42 00:02:14,110 --> 00:02:17,313 And therefore we get exactly that error here. 43 43 00:02:18,340 --> 00:02:21,500 So what we need to do is to call deposit 44 44 00:02:21,500 --> 00:02:23,730 actually on an account. 45 45 00:02:23,730 --> 00:02:27,680 And so what we want is for the result of this year 46 46 00:02:27,680 --> 00:02:30,380 to be again the account, 47 47 00:02:30,380 --> 00:02:31,430 right? 48 48 00:02:31,430 --> 00:02:32,653 And so let's do that. 49 49 00:02:34,150 --> 00:02:38,120 So all we have to do is return... 50 50 00:02:38,120 --> 00:02:39,350 this. 51 51 00:02:39,350 --> 00:02:41,833 Because this is of course, the current object. 52 52 00:02:42,980 --> 00:02:46,163 And now we do the same on all of them. 53 53 00:02:47,670 --> 00:02:48,503 Okay. 54 54 00:02:51,760 --> 00:02:53,023 And here the same. 55 55 00:02:54,520 --> 00:02:56,350 And that's it. 56 56 00:02:56,350 --> 00:02:57,890 So returning this 57 57 00:02:57,890 --> 00:03:00,930 will essentially make the method chainable. 58 58 00:03:00,930 --> 00:03:03,670 And this makes most sense, in methods 59 59 00:03:03,670 --> 00:03:06,360 that actually set some property. 60 60 00:03:06,360 --> 00:03:09,490 And so all of the three, that we just did this, 61 61 00:03:09,490 --> 00:03:11,130 actually do that. 62 62 00:03:11,130 --> 00:03:14,070 So this one sets the movements here. 63 63 00:03:14,070 --> 00:03:15,890 This one, actually two, 64 64 00:03:15,890 --> 00:03:18,763 and this one also changes the movement array. 65 65 00:03:19,610 --> 00:03:22,970 And so all of these, basically set some property. 66 66 00:03:22,970 --> 00:03:26,833 And so these are then very useful to make chainable. 67 67 00:03:28,010 --> 00:03:29,273 So let's see now. 68 68 00:03:30,660 --> 00:03:32,100 And... 69 69 00:03:32,100 --> 00:03:34,080 Well, we get no error, 70 70 00:03:34,080 --> 00:03:36,170 and we get loan approved. 71 71 00:03:36,170 --> 00:03:40,860 So that means that probably everything worked here. 72 72 00:03:40,860 --> 00:03:43,293 So let's take a look at our account here. 73 73 00:03:44,830 --> 00:03:47,483 Actually, let's call account getMovements. 74 74 00:03:49,600 --> 00:03:52,473 So that's why we have this method in the public API. 75 75 00:03:53,950 --> 00:03:57,090 And so indeed, all the deposits and withdrawals 76 76 00:03:57,090 --> 00:04:01,460 that we just did, are now in this Movements array. 77 77 00:04:01,460 --> 00:04:02,460 Great. 78 78 00:04:02,460 --> 00:04:06,940 So with this, you have yet another tool in your toolbox now. 79 79 00:04:06,940 --> 00:04:10,100 So experiment a little bit with this on your own. 80 80 00:04:10,100 --> 00:04:14,330 And maybe also when you start writing your own classes. 81 81 00:04:14,330 --> 00:04:15,220 All right. 82 82 00:04:15,220 --> 00:04:16,450 So with this one 83 83 00:04:16,450 --> 00:04:18,290 I actually showed you all there is 84 84 00:04:18,290 --> 00:04:22,960 to show about Object Oriented Programming in JavaScript. 85 85 00:04:22,960 --> 00:04:27,960 So what a long section here, but also a beautiful one, 86 86 00:04:28,240 --> 00:04:30,190 if you ask me. 87 87 00:04:30,190 --> 00:04:33,440 Now, just to really wrap up this section. 88 88 00:04:33,440 --> 00:04:38,270 Next up, I have a nice overview of the entire class syntax 89 89 00:04:38,270 --> 00:04:40,633 and then one final coding challenge. 7233

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.