Welcome back in the previous video we learn that when the keyword this is not inside of a declared object
its value is the global object.
Let's quickly demonstrate what I mean by declared object in case the previous video still left you a
bit confused.
Remember the material here is quite challenging.
So try watching these videos a couple of times and typing the code examples to reinforce your knowledge
.
I'm going to make an empty object called data.
What I have just done is declared an object the data object did not exist until I declared it.
Now I'm going to attach a property onto this object called instructor and set the value equal to LTE
.
Now our data object that we declared has a key with the value of elhi inside of it.
This is what we mean when we say inside of a declared object.
If that still confuses you that's all right.
It will make even more sense when we examine the second rule and see the keyword this inside of an object
.
Now let's finish up with the first rule.
As you can see here when we put the key word this inside of a function its value is still the global
object.
Remember we said that the global rule applies unless the keyword this is inside of a declared object
but it's inside of a function right now.
So when we call the function what is this the value returned is the window object.
Now let's look at one final example.
What happens if we attach properties to the keyword this when its value is the global object.
And remember when I say Global Object I mean the window object in different javascript environments
.
This value will change.
But since we're in the browser the global object is the window object.
Now let's take a look at our variables in this function in the variables in this function.
We are attaching a property onto the keyword this called Person and setting it equal to Ellie.
Since the key word this refers to the global object anything we attach onto it it becomes a global variable
which means we can use it outside of the function.
This may seem like something we might want to do but this is actually very bad practice.
Let's quickly revisit how variables and scope work in javascript.
I'm going to declare a variable called dog and said the value equal to Rusty.
I'm then going to make a function called meek person an inside declare a variable called person using
the VAR keyword and said it equal to code.
Since I declared this variable inside of the function and I am not returning it's value I do not have
access to it outside of the function.
However since I declared my dog variable in the global scope I have access to it everywhere.
Now we can get around the problem of not being able to access our person variable by omitting the VAR
keyword inside of our function.
However this is very bad practice.
It's generally best practice to declare all of our variables that we want to use in multiple functions
at the top of our code even if they do not have a value and then assign those values at a later time
in a function.
Unfortunately since the keyword this is the global object it's quite easy to accidentally declare global
variables inside of a function.
Let's see an example.
I'm going to make a function called mistake an inside set a property on the keyword this called bad
idea to be equal to the string.
Whoops.
Now I'm going to call that function and we can see that I have access to my bad idea variable.
I accidentally just made a global variable.
Thankfully in yes 5 the previous version of javascript something called strict mode was added to help
us.
Javascript developers read better cope when strict mode is enabled.
Which you can do by adding use strict in double quotes.
The value of the keyword this when inside of a function is undefined.
It's not the global object.
This means that if we try to attach properties onto it we get a type error.
Since we can't attach properties to undefined you might have seen this error quite a bit.
This stops us from accidentally creating global variables and allows us to use javascript best practices
.
In this video we reviewed the first rule for determining the keyword this and saw how we can accidentally
create global variables inside of functions.
We also saw how strict mode can prevent us from making this mistake in the next video.
We'll be discussing the second rule and what the value of the keyword this is when it is inside of a
declared object see there