Hey, remember the car from the earlier lesson, well, it's back and it's brought another car with
it.
So now we've got a Mustang and we've got a Honda Accord.
So we've got several different cars and we relatively want them to do the same thing.
But there are some differences.
There's different models.
There's different prices.
So we want them to be able to do the same thing where we can drive it.
But if we want to sell it, then there is a difference in the price as well.
So we want some of the functionality to be the same and others to be different.
And this is where we're going to be using objects and constructing objects, using the blueprint that
we learned in the last lesson.
So now it's your turn.
You can pause the video and use what we learned in the last lesson with the blueprint and create two
different cars.
I'll show you the solution coming up.
Let's go ahead and create our car constructor function.
So creating the function and we're going to uppercase car.
This is a constructor.
And then we're going to take in the values that are going to be different for each one of the cars.
So we've got color, brand, milk, price and year, and you might have a few other ones as well.
So let's start with these and then coming in.
And as we're constructing the object, we want to add in the color to equal to color and also have a
brand and equal it to brand.
Also make equal that to of course make makes sense to price.
Now you don't have to name them the same as well.
So now we've got a basic car that we've created and let's construct two different cars.
So we've got one, we'll call it Honda and this is going to be new car.
And then what are the values that we want to send in?
So first is color, so we want to be red.
The brand is going to be a Honda.
The make of it is an accord.
The price of the car is forty five thousand, say, and I have no idea how much it is.
So I'm just doing a rough ballpark estimate.
So forgive me if I'm wrong.
Next, let's do the same thing for the Mustang and this one is an older Mustang.
So the year is let's make just some adjustments here and maybe it's worth more money than a brand new
accord.
Not sure.
And update the color to be black, the make of it.
So it's up by Ford and the model is Mustang.
So let's see what happens.
Refresh.
And now when we output Honda, let's see what we've got.
We've got our car here.
So we've got all of the information.
And then, of course, we can make adjustments to it as well.
So if you want just this year, we can adjust the year as well.
And then look at Honda and you can see the year's changed.
So we've got all of the flexibility and we can work with these values as needed.
And also the same thing for Mustang.
So we've got two different objects.
So Honda and Mustang and we're essentially using that same template.
So now let's take this one step further and add in some methods.
And this is really the fun part.
And this is where you see really the power of creating it this way, because you can create the method
and use some of the values that are contained in here and really do some really cool things with it.
So I'm going to just simply console lock outs.
And in this case, we can use the tactics.
And what do we want to say?
Let's say I am driving my and then this is where we can add in the type of car.
So this is this brand.
So it's referencing whatever the current object is.
So if we change it and also let me add in the year and I'll show you that we can make the updates and
the changes and that we can use the year.
So just as we saw earlier, where we've got the year coming out here where we're able to update it,
it will also update within the method as well.
And OK, let's add in vroom, vroom.
So that's drive and let's refresh.
So now we've got our Honda and let's drive the Honda and see what happens.
I'm driving my 2020 Honda.
Vroom, vroom.
Let's take a Honda update our a year.
And how about we make it 20, 40 and go back to drive.
I'm driving by 2040.
Honda Vroom Vroom.
So this is in case you're watching the video way, way into the future in 20, 40.
So that makes a little bit more sense at that time.
Let's also add in a few other methods and you can have some fun with this, of course.
It's really just designed to help you practice and get more familiar with what you can do with JavaScript.
So another console message to use the tactics again.
And this time we're parking the car.
So parking the and we've already used let's instead of brand we'll use make and this shouldn't be drive.
This should be park.
So whatever actions you're taking, reference these methods.
And also if we want to sell it like so what if we want to sell it, we want to maybe get a certain percentage
of the price of the car that we paid for it.
So I want to sell it.
So let's make this one a little bit longer message I want to sell my for at least, and then this is
where we can calculation if we want so we can take the price and multiply it by point nine or something
like that for at least and put a dollar sign in front there.
I know this is getting fairly long.
Of course you can make your own since I paid and this is going to be the price again.
So this is the price that they paid.
And I want to get at least 90 percent of the value back.
So see what this looks like.
So Honda and if you want to sell the Honda, I want to sell my accord for at least forty thousand five
hundred since I paid forty five hundred forty five thousand for it.
Let's see what the Mustang is selling for and those same message calculations all done.
Maybe I want to park my Mustang.
So parking the Mustang.
I want to also maybe park the Honda Accord.
OK, I think you get the point.
So go ahead, try the suit, get comfortable with creating and constructing these objects.
And the nice thing about it is if all of a sudden you've got another car, all you need to do is enter
in these parameters and the rest of the stuff is the same.
So even if you had something like this and wheels and so we know that all of them will have four wheels.
So you can set some default values here as well, that there's four wheels that's got a steering wheel,
it's got a radio.
So all of the stuff that would come standard within the car.
And this makes it a lot easier than having to do the Honda and constructing all this stuff and then
Mustang and all of this stuff.
And if you have some changes, it's way, way easier doing it this way.