Welcome back.
In this video we'll be talking about the fourth rule for determining what the key word this is and we'll
recap all four rules to help you wrap your mind around this tricky topic.
The fourth rule for determining the value of the keyword this is when you see the new keyword.
The new keyword is a reserve keyword which does quite a few things.
We'll be covering the New keyword in more depth in the object oriented programming section.
But for now just know that when the new keyword is used a new object is created out of thin air.
The new keyword is used with a function and inside of the function definition.
The key word this refers to the new object that is created.
Finally when the new keyword is used an implicit return this is added to the function which uses it
.
That sounds like a lot but will cover it in great detail and the object oriented programming section
.
For now let's take a look at this code example.
We have a function called person which takes in two parameters.
First name and last name.
Inside of this function we're attaching properties on the keyword.
This equal to the values passed to the function.
You may be thinking hold on the value of the keyword.
This is the global object.
We saw that in a previous video and in fact you'd be absolutely right.
However the value of the keyword this changes when we use the new keyword.
Like we said before the keyword this now refers to an object that is created when the new keyword is
used.
We're storing that object in a variable called L-E and can then access the first name and last name
properties on it.
To recap we covered quite a lot in the section we examined the key word this which is a reserve key
word in javascript and its value is determined at execution.
We saw four rules global implicit explicit and the new keyword.
When the keyword this is in the global context or in a function it is either the global object which
is the window.
If in the browser or undefined if we're using strict mode when the keyword this is inside of a declared
object the value of the keyword this will always be the closest parent object.
When we lose the context of the keyword this we can explicitly set the value using call apply or bind
.
Finally when the new key word is used the value of the keyword.
This is set to an empty object and return from the function that is used with the new keyword.
Congratulations you made it through the series on the keyword this pat yourself on the back.
This is some complex material.
If you're still tripped up try to walk through the code examples and be sure to ask us any questions
.
We're always happy to help.
Thanks for watching