There are a few more things that we need
to learn about classes.
And so, let's actually create a new class now.
And as an example,
we are gonna use the bank account
that we implemented before, in the Bankist app.
Remember that?
So, let's say class Account, and then as always,
we need the constructor method.
And here each account should have the owner name.
It should have a defined currency,
and also, we want to pass in a pin.
And so, let's say this.owner is owner
and the same for currency and the pin.
So, we already know how to do all of this, right?
And so now let's create a new account here.
Let's say acc1, and so new account,
and let's say that I am the owner.
So, Jonas, then the currency is in Euro and the pin,
let's say, is this one?
All right.
So, indeed we have the three values that we passed
into the constructor, now inside of the object.
But now what about the movements array,
and maybe also the local.
So, we want to start always with an empty array
as the movements and the local,
we want to get from the navigator.language.
So, how should we do that?
Should we add a new parameter, like this?
So, movement, and then always pass in an empty array
in all the accounts that we create.
Well, that would work.
So, let's actually see that.
So, movement, actually movements should be equal
to movements, here as well.
And so now, as you see,
the movement is now this empty array,
however, it doesn't make much sense
to pass in this empty array,
into all the new accounts that we want to create, right?
So, we don't need this, and instead we can simply do this.
And so, this is something that we didn't do before,
but of course we can create even more properties
on any instance and properties
that are not based on any inputs.
And the same we can now do for the local
and set it to navigator.language, right?
And so now, all of that appears here as well.
And in fact, we can even execute any code
here in this constructor that we want.
So, let's say, "Thanks for opening an account,"
and then owner.
And so, when Jonas opens a new account,
then he's basically greeted with this message here,
coming right from the constructor.
All right, but now what about the deposits and withdrawals?
So, basically what about our movements?
Well, we could simply now go ahead and do this.
So we can take acc1.movements,
and then if we wanted to do a deposit,
we could push a value into this.
Let's say 250, and then when we want to withdraw something,
we would simply push a negative value.
And so, if we then take a look at our account here,
then of course we would see these movements right here
in our array.
However, it's not a good idea at all to do this.
So, instead of interacting with a property like this,
it's a lot better to create methods
that interact with these properties.
And that is especially true for important properties,
such as these movements here.
So, this will for sure avoid bugs
in the future, as your application grows.
So, let's now actually create a deposit
and a withdrawal method here.
So, deposit and inherit the value that we want to deposit.
And then this.movements.push,
the value that we pass into.
And now the same for withdrawal.
So, also a value.
And so here, we can now actually call this method
because it's actually gonna work basically the same way.
So this.deposit.
And so, as you can see here,
we can actually call other methods
inside of a certain method.
So, in withdraw, we are calling this.deposit,
but of course we still need to use the this keyword
to be able to access this other method.
And then here, we pass in minus the value
that we received in this function.
So let's try this now.
So instead of doing this,
we now can do it in a much nicer way.
So acc1.deposit 250, and then acc1.withdraw 140.
And so indeed,
our movements array looks just the same as before,
but now we are actually using this public interface
that we built here, right?
So, basically these methods here,
are the interface to our objects,
and we also call this API, remember?
So, we talked about this,
right at the beginning of the section.
So again, let's actually write this here.
So this is the public interface of our objects.
And so, yeah, this is a lot better
than having to manually manipulate
these properties outside of the object, as we did here.
Also this withdraw method here actually abstracts the fact
that a withdrawal is basically a negative movement.
So here as we did it manually,
we needed to pass in minus 140.
But now, as we do a withdrawal, it's of course,
a lot more natural to write
that simply 140 are gonna be withdrawn.
So, what I'm trying to say is that this minus here,
is something that the user of this object,
shouldn't be caring about.
And so now, we actually abstracted that away,
right into this object,
and in particular into this withdrawal method.
So, just a very small obstruction here in this case,
but it's still worth to notice.
All right, now, of course,
still there is nothing stopping someone on our team
from interacting with the movements array directly
and potentially making mistakes and introducing bugs.
So, simply the fact that we have these methods,
doesn't make it impossible to still do this.
And the same goes, for example, for the pin.
So, of course, we can access the pin
from outside of the account.
So, you see, but probably it shouldn't be accessible
from outside of the class, but yeah,
of course, right now it is accessible.
And this is actually a very real and very important concern.
So, it's not just something theoretical
that I'm telling you here.
And the same, of course, goes for methods.
So, let's say we have a requestLoan method here,
for some value.
And remember that this was also a functionality
of the Bankist application.
So, we could make the approval of the loan based
on some condition, and that condition could come
from some other method, let's say,
approveLoan, let's pass in a value here as well,
and this one will simply always return true.
So, I don't want to implement any complex logic here now,
but anyway, here we can now call this.approveLoan
with the value.
And so if the loan is approved,
then this.deposit of the value,
and we can lock something to the console.
So, "Loan approved" or something like that, okay?
Now, in the public interface,
we basically only want this method here.
So, we want to be able to do this here basically,
acc1.requestLoan, let's say of 1000.
And so, we see that the loan was approved
and it was pushed into our array.
But, of course, we are also able to do this.
So approveLoan.
And so this, of course, doesn't do anything,
but in the real world,
we should not even be allowed to access this kind of method.
So this is kind of an internal method
that only the requestLoan method should be able to use.
And so the reason why I'm telling you all this,
is basically just to justify
that we really need data encapsulation and data privacy.
And we talked about these topics in the introductory lecture
of the section, and so as always,
you can go ahead and review that,
but anyway, in the next video,
we will finally start implementing data privacy
and data encapsulation.
So let's move on there right now.