Now that we have a good idea of how the first rule works when the keyword this is outside of a declared
object let's see what happens when the key word this is inside of a declared object.
The second rule which we call the implicit or object rule states that when the key word this is found
inside of a declared object the value of the keyword this will always be the closest parent object that
was a lot of words.
Let's see what we mean by that by looking at the code on the slide.
Here we have a Person object but the key is a first name.
Say hi and determine context.
Let's start with the say hi method inside of this function we're returning the value of the string.
Hi concatenated with the value of the first name property on the keyword.
This Since we see that the keyword this is inside of a declared object we look at the closest parent
object to figure out the value of the keyword.
This we can see that the closest parent object is the person variable which means that the value of
the keyword.
This is the person object.
Now let's examine the determine context method.
This function should return true or false.
Notice that we're using triple equals which will always evaluate to a boolean.
What do you think this function will return.
Think about where you see the key word.
This is it inside or outside of a declared object.
We see that the keyword this is inside of the person object which means that it will equal the person
object.
So this expression should evaluate to true before moving on.
Take a second to review this code and put it in the chrome console or text editor to see exactly what's
happening.
Now we said in the implicit rule that the value of the keyword this will always be the closest parent
object.
But let's see how things get a bit tricky with nested objects.
Let's walk through this code.
Here we see that we have a person variable but this time the first name property is called.
Once again we have a hi method and determine context method.
Take a second and think about what those functions will return when called.
Now notice we have an additional key in the person object called dog.
The value of the donkey is actually another object.
So let's examine the nested object inside of the dog object.
We have a key of say hello which is a function that returns the string Hello joined with the key word
.
This using the two rules for determining the value of the keyword this pause the video and try to figure
out what the value of the keyword this inside of the say hello function is.
So would you come up with.
What is the key word this refer to the person object the dog object.
Or maybe the global object.
Well let's go back to our first rule which states that if the keyword this is not inside of a declared
object it will be the global object.
In this case we see that the keyword this is inside of a declared object called Person.
So we move on to the second rule.
The second rule states that the value of the keyword this will always be the closest parent object.
This is where things get a bit tricky even though the declared object is person.
There is an object inside it called Dog which is the closest parent object to the say hello method.
Now that we know that the keyword this inside of the say hello method refers to the dog object what
is the value of this stuff.
First name inside of the say hello method.
Since the dog object does not have a key of first name the value will be undefined.
So why don't we call the say hello method by typing person dog.
Say hello we can see it returns.
Hello undefined.
We can also see that our determined context function inside of the dog object is returning false because
the keyword this inside of the determine context function does not refer to the person object anymore
.
So how can we fix this or if we want our Say hello method to return.
Hello cold instead of hello undefined you would need some way of explicitly changing the value of the
keyword this and that is exactly what the call apply and bind functions can do for us in the next video
.
We'll see the third rule for determining the value of the keyword this using call apply and bind.
Things are getting exciting.
See you in the next video.