Hi everyone you might have just noticed that the voice you're hearing is not close.
So let me introduce myself.
I'm Ali and I've worked with Colt for the past couple of years at a few coding tools in San Francisco
and I'm really excited to teach you all some intermediate javascript in this video.
We're going to be talking about the key word this.
The key word this is a bit tricky to wrap your head around and learning intermediate javascript but
it is the foundation for object oriented programming and other trickier concepts like call apply and
bind which will cover in a later video to get started.
We're going to try to understand what the key word this is.
We're then going to analyze the four ways the key word this is determined so that we can always know
what the value will be.
Finally I'm going to try as hard as possible not to use the word this in a sentence.
Here we go.
So what is the key word this.
First off it is a reserved keyword in javascript which means we can't set it as the value of any variable
.
More importantly the value of the keyword this is determined by how a function is called.
We usually call this the function's execution context.
But the most important thing about the keyword this is that if you understand the four rules of how
the value of the keyword this can be determined global implicit explicit and new you will never have
a problem with the keyword this.
So let's get started with the most broad rule of them all.
The global context.
This rule applies when you see the keyword this outside of a declared object.
What do I mean by declared object.
Excellent question.
What we mean here is that there has not been an object defined which contains the keyword this.
You can also think of it as the keyword.
This is in the wild when we see the keyword this in the global context.
It's a value refers to the global object which in the browser is the window object.
In fact every variable that you declare in the global scope is actually attached to the window object
.
Let's see what that looks like.
I'm going to make a variable called person and also that equal to alley.
Now this variable person is actually attached to the window object which means the window person is
actually the same exact thing as this person variable that I need.
Now that we have an idea of what the window object is let's come back to the keyword this on the first
line we see the key word.
This is in the wild.
It is not inside of any object.
So what's its value.
You guessed it.
Or maybe you just saw in the comment It's the window.
Simple enough when the keyword this is outside of an object its value is the global object which in
the case of the browser is the window object.
So what have we learned so far.
The key word this is a reserved keyword that can be determined using one of four rules.
We saw a bit of the first rule which stated that when the keyword this is not inside of a declared object
its value is the global object in the next video.
We're going to see what happens when the keyword this is inside of a function.
So either