1
00:00:01,380 --> 00:00:07,980
Welcome back in the previous video we learn that when the keyword this is not inside of a declared object

2
00:00:08,790 --> 00:00:11,580
its value is the global object.

3
00:00:11,820 --> 00:00:16,980
Let's quickly demonstrate what I mean by declared object in case the previous video still left you a

4
00:00:16,980 --> 00:00:18,180
bit confused.

5
00:00:18,180 --> 00:00:21,250
Remember the material here is quite challenging.

6
00:00:21,420 --> 00:00:26,550
So try watching these videos a couple of times and typing the code examples to reinforce your knowledge

7
00:00:26,550 --> 00:00:28,410
.

8
00:00:28,470 --> 00:00:31,310
I'm going to make an empty object called data.

9
00:00:31,440 --> 00:00:38,650
What I have just done is declared an object the data object did not exist until I declared it.

10
00:00:38,670 --> 00:00:45,920
Now I'm going to attach a property onto this object called instructor and set the value equal to LTE

11
00:00:45,930 --> 00:00:46,610
.

12
00:00:46,620 --> 00:00:55,150
Now our data object that we declared has a key with the value of elhi inside of it.

13
00:00:55,230 --> 00:00:59,200
This is what we mean when we say inside of a declared object.

14
00:00:59,520 --> 00:01:01,750
If that still confuses you that's all right.

15
00:01:01,800 --> 00:01:07,770
It will make even more sense when we examine the second rule and see the keyword this inside of an object

16
00:01:07,770 --> 00:01:08,820
.

17
00:01:08,820 --> 00:01:10,780
Now let's finish up with the first rule.

18
00:01:11,010 --> 00:01:17,340
As you can see here when we put the key word this inside of a function its value is still the global

19
00:01:17,400 --> 00:01:18,290
object.

20
00:01:18,600 --> 00:01:25,800
Remember we said that the global rule applies unless the keyword this is inside of a declared object

21
00:01:26,190 --> 00:01:28,590
but it's inside of a function right now.

22
00:01:28,950 --> 00:01:36,270
So when we call the function what is this the value returned is the window object.

23
00:01:36,270 --> 00:01:38,340
Now let's look at one final example.

24
00:01:38,640 --> 00:01:46,060
What happens if we attach properties to the keyword this when its value is the global object.

25
00:01:46,110 --> 00:01:52,590
And remember when I say Global Object I mean the window object in different javascript environments

26
00:01:52,590 --> 00:01:52,640
.

27
00:01:52,680 --> 00:01:54,410
This value will change.

28
00:01:54,540 --> 00:01:59,730
But since we're in the browser the global object is the window object.

29
00:01:59,730 --> 00:02:05,340
Now let's take a look at our variables in this function in the variables in this function.

30
00:02:05,340 --> 00:02:12,100
We are attaching a property onto the keyword this called Person and setting it equal to Ellie.

31
00:02:12,420 --> 00:02:20,010
Since the key word this refers to the global object anything we attach onto it it becomes a global variable

32
00:02:20,370 --> 00:02:23,340
which means we can use it outside of the function.

33
00:02:23,340 --> 00:02:28,930
This may seem like something we might want to do but this is actually very bad practice.

34
00:02:29,670 --> 00:02:34,680
Let's quickly revisit how variables and scope work in javascript.

35
00:02:34,680 --> 00:02:39,780
I'm going to declare a variable called dog and said the value equal to Rusty.

36
00:02:39,810 --> 00:02:47,100
I'm then going to make a function called meek person an inside declare a variable called person using

37
00:02:47,100 --> 00:02:50,610
the VAR keyword and said it equal to code.

38
00:02:50,790 --> 00:02:57,420
Since I declared this variable inside of the function and I am not returning it's value I do not have

39
00:02:57,420 --> 00:03:00,430
access to it outside of the function.

40
00:03:00,450 --> 00:03:07,410
However since I declared my dog variable in the global scope I have access to it everywhere.

41
00:03:08,160 --> 00:03:13,830
Now we can get around the problem of not being able to access our person variable by omitting the VAR

42
00:03:13,830 --> 00:03:16,020
keyword inside of our function.

43
00:03:16,020 --> 00:03:18,900
However this is very bad practice.

44
00:03:18,960 --> 00:03:24,900
It's generally best practice to declare all of our variables that we want to use in multiple functions

45
00:03:25,110 --> 00:03:31,590
at the top of our code even if they do not have a value and then assign those values at a later time

46
00:03:31,680 --> 00:03:34,370
in a function.

47
00:03:34,380 --> 00:03:41,550
Unfortunately since the keyword this is the global object it's quite easy to accidentally declare global

48
00:03:41,550 --> 00:03:44,400
variables inside of a function.

49
00:03:44,400 --> 00:03:47,340
Let's see an example.

50
00:03:47,340 --> 00:03:54,300
I'm going to make a function called mistake an inside set a property on the keyword this called bad

51
00:03:54,300 --> 00:03:57,060
idea to be equal to the string.

52
00:03:57,100 --> 00:03:58,480
Whoops.

53
00:03:58,770 --> 00:04:05,400
Now I'm going to call that function and we can see that I have access to my bad idea variable.

54
00:04:05,400 --> 00:04:09,390
I accidentally just made a global variable.

55
00:04:09,390 --> 00:04:16,290
Thankfully in yes 5 the previous version of javascript something called strict mode was added to help

56
00:04:16,290 --> 00:04:16,440
us.

57
00:04:16,440 --> 00:04:21,870
Javascript developers read better cope when strict mode is enabled.

58
00:04:21,960 --> 00:04:26,460
Which you can do by adding use strict in double quotes.

59
00:04:26,670 --> 00:04:31,760
The value of the keyword this when inside of a function is undefined.

60
00:04:31,800 --> 00:04:34,530
It's not the global object.

61
00:04:34,530 --> 00:04:38,870
This means that if we try to attach properties onto it we get a type error.

62
00:04:38,940 --> 00:04:44,270
Since we can't attach properties to undefined you might have seen this error quite a bit.

63
00:04:44,280 --> 00:04:50,440
This stops us from accidentally creating global variables and allows us to use javascript best practices

64
00:04:50,450 --> 00:04:51,490
.

65
00:04:52,140 --> 00:04:57,660
In this video we reviewed the first rule for determining the keyword this and saw how we can accidentally

66
00:04:57,720 --> 00:05:00,860
create global variables inside of functions.

67
00:05:00,870 --> 00:05:06,410
We also saw how strict mode can prevent us from making this mistake in the next video.

68
00:05:06,450 --> 00:05:12,330
We'll be discussing the second rule and what the value of the keyword this is when it is inside of a

69
00:05:12,330 --> 00:05:14,190
declared object see there
