Hey, welcome back, kid.
I know the last lesson was fairly lengthy in this lesson, I'm going to try to keep it short and I do
apologize for the last one.
This lesson we are going to be looking at prototype in JavaScript.
So object prototype is a property of an object constructor and it's also the end of a prototype chain.
So all JavaScript objects inherit properties and methods from the prototype.
So this gives you the ability to add to a particular object and add additional functionality and methods
to those objects.
And of course, you have to declare it before you try to use it.
And objects such as the DETE object so you can update and date prototype can inherit within the date
object, so you can use existing ones as well as create your own.
And there's an example of this and there's more information about prototype, of course, at the Mozilla
Developer Network.
So more information about just different ways to create prototypes, said work with prototypes.
They've got an example here.
So object prototype value of so this is extending on value of and adding in different functionality
to those so you can use those and utilize them.
So they've got an example here of the person and updating that prototype with greet.
So now when you call to person, so using it as an object, you can extend on that person and have a
greeting coming back.
So let me show you a little bit more, opening up the editor.
Let's create our first function and we can call this one just person.
So we've got first and last as parameters that are coming into the function.
And then we're using this first name is going to be equal to whatever the value of first is in this
last name is going to be equal to whatever the value of last is.
So it's going to be a quick and easy way to create an object and then we're going to extend on that
person prototype.
So if we create a new object and we could just call it meat and we can use new person, so extending
on that object that we have, we can pass in the first name as well as the last name that we can use
in the object if we consult, like about me.
What do you think is going to happen?
You're going to see that we've got person and we've got first name, last name so we can use that.
So let's create a prototype on person so that a person object and using prototype, we're extending
on that.
So we're going to add in a method called full name and function.
So it's going to be a method where it's going to use the values are contained within the object.
So seeking this and it's taking this first name value and adding with space in the middle last name,
and that's what's going to return back as the prototype.
So now that gives me the ability.
So if I save that and I'm simply outputting it as an object into the console, but now if I do console,
log me full name or simply just call back to see full name.
So I'm using the prototype and it's returning back by fullName that have passed on from the object that
was created.
So there is a challenge to this lesson and we did mention that the date object in JavaScript, so you
can add a prototype to it.
So extending on a date where you can have the date method and then you can simply add days to it.
So the objective is to create a prototype on the date object where you can add days to it specifying
a parameter that comes in.
And this is the number of days that get added.
And so go ahead and try this out and I'll walk you through the solutions.
You can pause the video here and I'll walk you through the solution.
This could be a really short solution.
So taking our DETE prototype, so the date object, because that exists in JavaScript and then using
prototype to extend on it, we're creating a method called add days and then setting that up as a function.
So it's taking in a parameter of days and it's going to take that and make a calculation.
So new Dete, it's going to take the current value of and then add days to it.
We're going to multiply this.
So this is eighty six for E five.
So that should give us the amount of milliseconds in the days and basically allowing us to make that
calculation.
So now if we do console log and the nice thing about prototype is you can use it over and over again
so we can add days to it and we can specify how many days we want to add in to the date so that when
we refresh it, we see we automatically add seven days into the current date.
Today is the third.
We're coming back at the tenth.
If I add seventeen and so we can do 20, we can do a hundred and whatever we want to do.
So just save that.
Refresh that.
So that's coming into July 18th and we can even out a thousand.
So refresh going up to 20, 22 and so on.
So coming up next is try and catch such a very interesting one that you can utilize in JavaScript.