When we create objects, generally, it reflects a real object where there is content that has a natural
grouping together.
So if we take something that when we look out the window, we see a car, a car is a great example of
an object.
So all the cars relatively the same.
They're all going to have four tires, but each one of these cars.
So one might be a different make, like a Mustang a might be a different brand as well.
There's different colors.
There's different years.
They're made, there's different values that they have some different costs, prices as well as they
might have some different functions or methods that they are able to do, that some of the other cars
might not be able to do.
And so this is ideal when you think of objects in order to place all of that information into an object
format that then you can utilize.
So the objective of this lesson is to try this out.
So look at the car that we've got below, create an object around that car and then add in the functionality
where if we do my car drive, then it says it returns back in the console.
I'm driving Mustang referencing the make vroom vroom and then also add in a park method where we can
do the my car park and then it can say the Mustang is parking or the Ford is parking and use some of
the values that are contained within the object.
So you can pause the video here.
Try this out and I'll walk you through the solution.
Coming up, I hope you had an opportunity to construct that car and you could use another car, too,
if you didn't want to use the Mustang, of course.
So first of all, we need to clear the myCar object so it's got nothing in it.
Now, there's a number of ways that you could have accomplished this.
You could have had this all in one, or you could have had the mycar creating that main container object
and then add to it.
So that's the way that I'm going to do it.
I'm going to add in color.
And either way, that would have been correct if you did it either way.
So in this case, we're just going to do it on separate lines.
So we've got my car.
What else did we have?
We had Brand as well.
So the brand for it is a Ford and then my car.
And we'll also do a make and the make of this.
So we've got a lot of information that's contained within the my car and it's all relevant.
It's all within that same grouping.
And it's really easily to see how this fits together.
So let's say you want it to get information about the car and you want to get a bunch of different cars.
You want to get the prices of those, the mix of them and so on.
So this is where the logic of when you create these objects that you can group things together.
And this is where you got to think of it logically, how you can group things together and how all of
this information can fit together.
So we did have some values here.
So we've got a color brand, Mieke, so let's try that out.
And usually I do like to try it out in the console just to make sure that things are working as expected.
So so we've got all this information being dropped into the object.
So the other part of this challenge, so of course, there was a two part to this challenge.
And the other part is to add in some methods and we did want one to drive.
So this is setting up that anonymous function and then doing whatever you want it to run.
So the block of code that you want to run is contained within this function.
And for this time, we're not going to be returning anything.
We're just putting it into the console so we can use template literal again as well as we did before.
So I have a message.
So I'm driving and then let's add in the make.
And that's the nice thing about it, is that we can reference this and return back all the information
that we've got contained within the object.
So we've got this make I'm driving where it should say, driving my Mustang because you're driving it
and then you can have you could use any any piece of information that's contained up here.
And then we just simply had vroom vroom.
So I think that's what a sound a car makes there.
So we've got the drive.
And then there was one more that we wanted to add in, and that was my car park.
And so, again, anonymous function.
And what block of code do you want to run within this function?
So we just want to have a console message and I'll use the template literals against a little batiks
there.
And we can say parking the car or instead of car, let's use some of the information contained within
the object so we can use this.
Let's use parking the Ford now, go into the console and see what's contained within the car.
So you've got all of this information, plus you've got the two methods as well.
So let's try one of those methods and one was drive.
And so don't forget to include the rounded brackets.
Otherwise you're not going to invoke the function.
So it's just going to return back that function.
That's not what we want, we want to make sure that we invoke the function and we see I'm driving my
Mustang, vroom, vroom, and maybe we could even add a few more O's there.
So it makes it a little bit more powerful.
Let's try the other one as well.
So we've got my car and park and see what happens parking the Ford.
So everything is working perfectly.
So go ahead, try the suit, get familiar with it.
You can even challenge yourself.
Add in some more information contained within the car and as well add in some additional methods in
order to get a fully functional car.
You can also change the car as well and have different functions, different values.
And we're going to be revisiting this later on when we look at more object oriented programming.
So for now, get familiar with creating this type of object.