Jonas: Welcome back to your first coding challenge
of this section.
So now it's time to finally put everything that you learned
in the section so far into practice.
So in this section,
I want you to use a Constructor Function
in order to implement a very simple car all right?
So for this purpose, a car has just a make
and a speed property,
and the speed is simply the current speed of the car
measured in kilometers per hour,
then I want you to implement an accelerate method
that will increase the car speed by 10,
and it would also log the new speed to the console,
then another method called brake,
which will then decrease the speed
but only by five all right?
And finally I want you to use this test data down here
to implement these two cars.
So basically to create two car objects
and then experiment with calling the two methods
that we just created on each of the objects.
And of course you should use all the knowledge
from the previous lectures to doing this.
So I hope that this is a fun challenge
that's not too hard.
So take all the time that you need
and I see you back here when you're ready with my solution.
All right so hopefully you did that successfully,
so let's get started here,
so we create the Constructor Function for car
and one more time with a capital C
and then we want each car object to have a make and a speed
and so that's the arguments
that we're gonna pass into this function.
And then just as before under this property,
we will set the make to the make that we receive
and the speed,
to the speed that we receive as well.
And so this is basically always gonna work the same way.
So as I mentioned,
this is a pattern that developers came up with
and so now we just simply follow this
like a recipe all right?
And with this, we can actually already create our two cars,
so let's say the BMW is a new car
and so remember this new operator here
is what's gonna enable all of this to work.
And so here the make is of course BMW
and the speed is 120 kilometers per hour,
which for my American friends is 75 miles per hour
I believe
so the next one is gonna be the Mercedes.
So that's new car
and at speed, 95 kilometers per hour okay?
So let's just take a look
and so yeah,
that appears to be working just fine.
And so now let's implement our two methods here,
so accelerate and brake
and of course we are not going to add the method right here.
So we will not do this .accelerate
because as we just learned before,
that would be terrible for performance,
especially if we had many, many car objects.
So instead we will of course make use
of prototypal inheritance.
So we take the prototype property of car
and to that, we add the accelerate property accelerate
that's correct
and we make it a method by passing in a function
or actually by specifying a function here.
So once again,
we can use the This keyword in here
because that is simply gonna be the object
on which the method will be called
and so that object will have the speed property
and so here we can then increase that by 10.
And then we can also log that to the console
and we can even make use of course of the make.
So this one wasn't specified in the text,
but we can log a nice string, like this
is going
at this.speed
and then kilometers per hour
but again this part,
you wouldn't have to implement like this.
And now let's just take this here
for the brake because it's gonna be pretty similar,
so brake,
and all we do then is to
decrease the speed by five all right?
And now let's just call this on the BMW a couple of times,
so bmw.accelerate,
and let's do that three times.
And so after three times,
the BMW is going at 150 kilometers per hour,
so apparently that works just fine.
Let's just add a brake here in the middle
and so now we reduce the speed a little bit
and then back up to 145 now okay?
And that's actually it,
that's all we had to do in this challenge,
and maybe with this example,
you can start to see the advantages
of object oriented programming in action.
So all the advantages that we talked about
right in the first lecture of the section,
and essentially the part where we learned
that in object oriented programming,
we pack both the functionality and the data into objects.
And so that's exactly what we have here,
so each of these objects,
so BMW and Mercedes contains the state of the car,
so the current speed and the make,
and it also contains the functionality
to manipulate its own data,
so the accelerate and brake methods in this case
are able to manipulate the speed, right?
And at the same time,
these two methods also form the public interface
of this object, right?
So right now the rest of our code
can interact with these objects
by calling the brake and the accelerate methods.
Now in this case, of course,
this is just a very small and isolated example,
so it can still be hard to see the advantages of this,
but maybe you can take a minute or two
and compare this style of coding
with the code that we have written up until this point
in the course.
All right, so at some point you will see
that this is really a completely different way of thinking
about our code,
but anyway that's it for this challenge.
In the next video,
we are gonna talk about ESX classes,
so I hope to see you there soon.