Welcome back in the end of the last video we saw that when we called our constructor function we did
not return anything.
The entire purpose of a constructor function is to construct objects.
So when we call the function we'd really like to have a new object be created.
Unfortunately that's not happening right now.
But the good news is we can fix our problems by using the new keyword when we use the new keyword.
Everything magically works.
The first house variable is now an object with the bedrooms bathrooms and number of square feet property
.
But let's see just exactly what the new keyword does.
Just like the key word this understanding with the new keyword does can be quite tricky.
But review the slides.
Remember these rules and most importantly try coding a couple of examples.
It will really help your attention of the material.
We see that the new keyword is being used with a function.
In fact the new keyword must be used with a function or else we'll get a type error.
Let's see why that's the case.
Examining just what the new keyword does first.
It creates an object out of thin air.
Second it sets the value of the keyword this in the constructor function which is being used with.
To be that the object that was just created.
Third it adds an implicit return.
This at the end of the function so that the object created using the new keyword can be returned from
the function.
Finally and this one is the most tricky.
It adds a property on the empty object which can be accessed using double underscore Prato double underscore
this double underscore is commonly called Dunder.
So this property is also known as Dunder Prato the Dunder Prato property links the object that was just
created to the prototype property on the constructor function.
That last sentence may have seemed like a blur but don't worry too much for now we're going to cover
this in painstaking detail.
For now let's practice by making a constructor function for a dog.
Each dog should have a name and an age as a bonus and a function for each dog object created from the
function called Bark.
This function should cancelled out log the name of the dog added to the string just barked pause the
video and try to figure this out.
So what did you come up with.
Hopefully it looks something like this.
Here we have a constructor function for a dog.
This function takes in two parameters.
Name and age.
Remember that you can mean your parameters anything.
But since we want to attach properties onto the object that will create called name and age we should
probably name our parameters.
The same thing inside of this function where setting properties on the keyword this equal to the value
of what is passed to the function as well as adding a function called Barch on to the keyword this which
canceled out logs the first name property concatenated with the string just parked on the next lines
.
We're creating two variables rusty and Feydeau and setting them equal to the value of the dog constructor
function being invoked with the new keyword.
Now before we recap with a new keyword does pause the video and see if you remember any of those four
things.
First it creates an empty object out of thin air.
Second it then sets the value of the keyword.
This in the function which is being used with to be the empty object that was just created.
Third it adds an implicit return.
This at the end of the function so that the object created using the new keyword can be returned from
the function.
Fourth it adds the Dunder Prato property onto the object that was just created.
In this video we examine how to use the keyword new to create objects from constructor functions.
We learned about the four things that the new keyword does which I highly encourage you to review because
we'll be covering this in much more depth and knowing the fundamentals is essential in the next video
.
We're going to talk about using more than one constructor function and how we can refactor our code
to make it even cleaner.
See that