You remember that in previous videos we have referred to the special care, this and in this lecture,
I'm going to talk about what exactly this word means.
It is also one of the important aspects in JavaScript, but at the same time, it's a bit confusing,
especially for new JavaScript developers.
The key word, this is very frequently used.
So you need to really understand how it works under the hood of the program.
Languages like Java C Sharp.
Use this keyword as well.
But in JavaScript it's a bit different topic.
OK, let's recall what happens when the page is loaded.
You already know that global execution context is created and it creates global window object anti's.
And on global level this completely equals to window object.
When we use this keyword inside a regular function in regular I mean usual function, which is not a
method created inside an object.
And then we invoke dysfunction.
Local execution context is created.
It defines this, which still refers to global window object, and in council we get window object.
So again, in the regular functions, which are not the methods of an object, this keyword points to
global window object.
But when we deal with method in objects, then something different happens.
Suppose that we have an object person with properties, first name and last name and method, get full
name, which returns full name of a person in order to get a full name.
We need to access first name and last name using dot operator.
That's why you see here per dot.
First name and last name.
When we invoked this method, then we get John Smith in council, if we change person into this, then
we will still get John Smith in council.
So how this happened in previous example, this Keywood referred to global.
We do object in this case when we call the method of object that the local execution context is created.
It defies keyword this.
But at this point it refers to object person.
That's why we get the same result in council.
So using keyword this insight, a method of object means that it completely equals to match an object.
All right.
Go to Brackett's and write some codes, create a function, function a
then write.
Cancel that log.
This.
Called the function.
Reload and you see that we have a window object, as we expected.
So in a regular function, this keyword refers to global window object and you see that we have true.
So in this case, this could act like it was in global execution context.
OK, now create another function inside, function A and call it function B, then inside the curly
braces.
Right, cancel that log of this.
And then consider this triple equals to window in function B, reload, and we get the exact same result.
So it doesn't matter if we create function on global level or incite other irregular functions.
This keyword always refers to global window object and acts like in global execution context.
All right, now, let's recall our example about object person, so right, variable per equals an object
and then create properties.
First name, John.
That last name, Smith.
Right, the method get full name.
Then create function.
And return.
Her first name.
Plus, this space.
And then plus last night.
The morgue get full name method, so right, Peridot, get full name.
Reload and we have John Smith.
OK, now, right above, cancel that log.
This triple equals Per.
We need to write it before a return statement because it terminates execution of codes which are written
after it.
So reload the page and you see that we have true.
That's because, as we said, inside the method of object, this refers to the motion object, which
is person here, and therefore we can use this instead of person.
So right here, this is your first name.
And then these last name.
Reload and he see that we have exact same result.
All right, now I want to show you an interesting and important thing regarding this quote, I'm going
to change a return into console the clock because I need to write some functions below it.
Also, get rid of cars that look from here.
Now create function.
Function great and then inside the price is right, cancel that log.
Hi, plus.
These first name.
And then call the function right, great.
So here we have another function inside the method of object person, and you may expect that it should
give us high John.
Watch.
We got high, undefined, this keyword in search function grid couldn't access on the property first
thing.
The reason is that people who created JavaScript decided that this keyword inside their regular function
to refer to global, we no object.
So it doesn't matter whether this function is on global level or inside an object method if we create
the variable with the same name, first name and assigned value.
Bob.
Then we will get high, Bob.
Variable first name is created in global scope.
Therefore, it's a property of global window object, which is referred by keyword this, and that's
why it would got high.
But some of the developers criticized the decision and they think that in this case, this should refer
to object person.
But it's the way how JavaScript works.
We can easily fix that, create variable and call it that.
Sometimes developers call it self.
So you can feel free and call any of them you like, then assign a value of this.
We don't need it anymore, so let's get rid of that.
Because of that at this point, I mean, inside a method, this keyword refers to object person that
using that variable we maintain the reference to object person and the last thing to do is to change
this into that.
Reload and you see that we have high John instead of Bob.
All right, the last thing I'm going to show you is how to borrow from object person the method, get
full name for that, create another object and call it person one.
Then at the properties, first name, right, Niek and last name, though.
Now, using dot notation, let's create get full name for person one right person, the one that got
full name.
And assigned to value.
Her daughter get full name.
So because of that, we have used keywords inside gatefold name method that this fact allows us to borrow
the method from object person and it will work perfectly for personal one in order to invoke this method.
We need to write per one dot, get full name.
So you see that it works for a person one, and we have Nick DDO and Heinicke from Object Person one.
Makes sense.
All right, you need to understand this concept very well, so I recommend to think about it again,
test these codes on your own.
Think of new examples and practice.
Generally, how JavaScript works under the hood is a bit confusing and unknown for many developers.
So if you are new and have some troubles with the section, you can watch the videos or feel free and
ask any question you want anytime.
I'll be glad to help.
See you in the next video.