-: Do you remember how we chained array methods
one after another, for example filter map and reduce?
So by chaining these methods,
we could first filter an array,
then map the result.
And finally reduce the results of the map,
all in one line of code.
And we can actually implement the same ability
of chaining methods in the methods of our class.
And so let's go do that now.
And actually, this is extremely easy to do.
So all we have to do is to return the object itself
at the end of a method that we want to be chainable.
So let's say that we wanted to do this.
So let me take that away from here.
So chaining.
So let's say we wanted to do account,
and then deposit,
let's say, 300.
And then right afterwards, we wanted
to deposit again on the same account.
So we would like to call deposit again, right away,
let's say 500 this time.
And then immediately after that,
let's say we want to withdraw 35.
Maybe we also want to request a loan in the middle of this.
So request loan, let's say 25,000.
And then finally, we want to withdraw some more,
let's say 4000.
So right now, this is not gonna work.
So let's see the error.
And so the problem here is that...
this one here will work.
But this will then return nothing.
Because let's see,
the deposit method does return undefined
because we're not returning anything explicitly here.
And so then undefined gets returned.
And so then here, in this next one
we are trying to call the deposit method on undefined
which is the result of all this.
And therefore we get exactly that error here.
So what we need to do is to call deposit
actually on an account.
And so what we want is for the result of this year
to be again the account,
right?
And so let's do that.
So all we have to do is return...
this.
Because this is of course, the current object.
And now we do the same on all of them.
Okay.
And here the same.
And that's it.
So returning this
will essentially make the method chainable.
And this makes most sense, in methods
that actually set some property.
And so all of the three, that we just did this,
actually do that.
So this one sets the movements here.
This one, actually two,
and this one also changes the movement array.
And so all of these, basically set some property.
And so these are then very useful to make chainable.
So let's see now.
And...
Well, we get no error,
and we get loan approved.
So that means that probably everything worked here.
So let's take a look at our account here.
Actually, let's call account getMovements.
So that's why we have this method in the public API.
And so indeed, all the deposits and withdrawals
that we just did, are now in this Movements array.
Great.
So with this, you have yet another tool in your toolbox now.
So experiment a little bit with this on your own.
And maybe also when you start writing your own classes.
All right.
So with this one
I actually showed you all there is
to show about Object Oriented Programming in JavaScript.
So what a long section here, but also a beautiful one,
if you ask me.
Now, just to really wrap up this section.
Next up, I have a nice overview of the entire class syntax
and then one final coding challenge.