Jonas: Remember how we chained array methods
one after another,
for example, a filter, map and reduce.
Now by chaining these methods,
we could first filter an array, then map the result,
and then reduce the results
of the map all in one line of code.
And we can actually implement
this exact same ability of chaining methods one
after another in the methods of our classes.
So let's say that instead of doing this,
so always acc1.deposit and then .withdraw,
and then whatever we want to do next.
So let's say that we just wanted to do it like this.
So deposit the 300, then .withdraw the 100.
Then let's say we want to withdraw another 50,
then maybe request a loan of 2,500
or 25,000.
And then let's say we want to withdraw another 4,000.
And here in the middle we might even want
to check our movements to get movements.
And again, doing all this in one go.
And this is actually a very common pattern
that you will see in object oriented programming,
for example, when you use some external libraries
as we're gonna do in the next section.
So again, this kind of thing is
what you will many times see in JavaScript
whenever you work with object oriented programming
in some sort.
So basically just starting with the object here,
then deposit, and then immediately on the
result you want to withdraw.
And then immediately on that result, you want
to withdraw again and so on until the end of the chain.
So how could we implement this?
Well, let's actually think about it.
So again, what we want to do in this line
of code is withdraw on the result of this.
Now we can only call these methods on an account object like
this, right?
So basically in order to be able to run this line of code,
this here needs to become an account object as well.
And the same for this one
and all the other methods here, right?
Now if that sounds confusing, don't worry
because it's actually extremely easy to do this.
So all we have to do is
to return the object itself at the end of each
of the methods that we want to be chainable.
So let's just do that on all of them.
And so let's start here.
Well actually this is not a good place to do so.
So let's leave this one here to the end.
So yeah, let's take this one out of the chain.
Let's just work with the other ones for now.
And so yeah, what I just said is that all we need to do is
to return the object again.
So that's just this, return this.
So return this.
Then here, this one isn't even part of the public API.
So no need there.
And then also return this here.
And of course, outside of the if, so, always at the end.
So the very last operation
inside the method needs to be this.
Now here we get this error.
this.approveLoan is not a function.
So this is a bug from the last lecture still.
So here, of course, we made this approved loan method
private, and so we need to change the name
of it now here as well.
All right? But now it worked.
And so let's check our movements here.
And indeed everything works correct.
So all these five movements are indeed now
represented right here.
So let's just quickly recap why this actually works.
So as we said in the beginning, these methods here,
all of them of course need to be called on
an instance of this class.
So in this case, this acc1 right here.
So in order for this one and this one and this one
and this one to work, they need
to be called also on an account object.
And what is this account object.
Well, inside these methods, it is just this.
And so if
after this first reposit here, we just return this, then all
of the thing here just becomes the account object again.
So after this method here,
this whole part becomes just acc1 again.
And so then on that we can call the withdraw function.
And after that finishes,
then this whole thing here acc1 again.
And then on that the withdraw function can be called
and so on and so forth.
So all the way until the end.
Now here in the middle, we just have this problem
that get movements actually also returns the
movements themselves.
So we cannot, of course return two things here.
So we cannot return the movements array
and then also this in order to make this method chainable.
And so if we want to keep this here
and we do want that, then this means
that this method here is not chainable.
Let's just write that here as a comment,
not chainable, meaning
that we cannot call the other
methods on it afterwards, like these other ones.
So it will work of course at the end.
So if we place this here, then it will work because
after this last withdraw, then all
of this here will be the acc1.
And then on that, of course we can call get movements.
Now we cannot see that here anywhere,
but if we just store that, so we could do that.
So for example, movements.
And then the result of that would just be
the movements array at the end
of all these operations like this.
And indeed here they are, so the first deposit,
then the two withdrawals, then the loan,
and then the last withdrawal.
Beautiful.
So this is really extremely simple to implement
as you just saw, but it can be very useful in some cases.
So just experiment around with this in your own classes.
Now this makes most sense in methods
that actually set a property.
And so this way you can then set them all together.
So just like we did here.
So all of these do set properties while this one here reads
a property or actually a class field,
but that's really the same.
And so that's also one
of the reasons why we didn't make this one here chainable.
So this is yet another really nice tool in your
object-oriented programming toolbox.