1
00:00:00,950 --> 00:00:06,390
Now that we have a good idea of how the first rule works when the keyword this is outside of a declared

2
00:00:06,420 --> 00:00:12,940
object let's see what happens when the key word this is inside of a declared object.

3
00:00:12,960 --> 00:00:18,600
The second rule which we call the implicit or object rule states that when the key word this is found

4
00:00:18,660 --> 00:00:26,120
inside of a declared object the value of the keyword this will always be the closest parent object that

5
00:00:26,130 --> 00:00:27,100
was a lot of words.

6
00:00:27,300 --> 00:00:30,640
Let's see what we mean by that by looking at the code on the slide.

7
00:00:30,660 --> 00:00:33,590
Here we have a Person object but the key is a first name.

8
00:00:33,630 --> 00:00:35,910
Say hi and determine context.

9
00:00:36,090 --> 00:00:41,350
Let's start with the say hi method inside of this function we're returning the value of the string.

10
00:00:41,350 --> 00:00:45,960
Hi concatenated with the value of the first name property on the keyword.

11
00:00:45,960 --> 00:00:52,650
This Since we see that the keyword this is inside of a declared object we look at the closest parent

12
00:00:52,740 --> 00:00:55,710
object to figure out the value of the keyword.

13
00:00:55,710 --> 00:01:02,640
This we can see that the closest parent object is the person variable which means that the value of

14
00:01:02,640 --> 00:01:03,210
the keyword.

15
00:01:03,210 --> 00:01:07,010
This is the person object.

16
00:01:07,020 --> 00:01:09,520
Now let's examine the determine context method.

17
00:01:09,540 --> 00:01:11,970
This function should return true or false.

18
00:01:11,970 --> 00:01:16,260
Notice that we're using triple equals which will always evaluate to a boolean.

19
00:01:16,260 --> 00:01:18,320
What do you think this function will return.

20
00:01:18,330 --> 00:01:19,940
Think about where you see the key word.

21
00:01:19,940 --> 00:01:24,290
This is it inside or outside of a declared object.

22
00:01:24,390 --> 00:01:29,850
We see that the keyword this is inside of the person object which means that it will equal the person

23
00:01:29,940 --> 00:01:30,780
object.

24
00:01:30,900 --> 00:01:35,390
So this expression should evaluate to true before moving on.

25
00:01:35,430 --> 00:01:41,100
Take a second to review this code and put it in the chrome console or text editor to see exactly what's

26
00:01:41,100 --> 00:01:41,840
happening.

27
00:01:42,200 --> 00:01:47,370
Now we said in the implicit rule that the value of the keyword this will always be the closest parent

28
00:01:47,400 --> 00:01:48,300
object.

29
00:01:48,510 --> 00:01:52,000
But let's see how things get a bit tricky with nested objects.

30
00:01:52,500 --> 00:01:53,940
Let's walk through this code.

31
00:01:53,970 --> 00:01:58,960
Here we see that we have a person variable but this time the first name property is called.

32
00:01:59,040 --> 00:02:02,700
Once again we have a hi method and determine context method.

33
00:02:02,700 --> 00:02:06,570
Take a second and think about what those functions will return when called.

34
00:02:06,570 --> 00:02:10,770
Now notice we have an additional key in the person object called dog.

35
00:02:10,890 --> 00:02:14,110
The value of the donkey is actually another object.

36
00:02:14,190 --> 00:02:19,180
So let's examine the nested object inside of the dog object.

37
00:02:19,200 --> 00:02:24,360
We have a key of say hello which is a function that returns the string Hello joined with the key word

38
00:02:24,350 --> 00:02:24,470
.

39
00:02:24,480 --> 00:02:32,130
This using the two rules for determining the value of the keyword this pause the video and try to figure

40
00:02:32,130 --> 00:02:38,870
out what the value of the keyword this inside of the say hello function is.

41
00:02:38,880 --> 00:02:40,230
So would you come up with.

42
00:02:40,410 --> 00:02:44,230
What is the key word this refer to the person object the dog object.

43
00:02:44,280 --> 00:02:45,720
Or maybe the global object.

44
00:02:45,930 --> 00:02:51,090
Well let's go back to our first rule which states that if the keyword this is not inside of a declared

45
00:02:51,120 --> 00:02:53,820
object it will be the global object.

46
00:02:53,820 --> 00:02:58,520
In this case we see that the keyword this is inside of a declared object called Person.

47
00:02:58,770 --> 00:03:01,090
So we move on to the second rule.

48
00:03:01,170 --> 00:03:07,050
The second rule states that the value of the keyword this will always be the closest parent object.

49
00:03:07,050 --> 00:03:11,850
This is where things get a bit tricky even though the declared object is person.

50
00:03:11,940 --> 00:03:19,060
There is an object inside it called Dog which is the closest parent object to the say hello method.

51
00:03:19,110 --> 00:03:25,680
Now that we know that the keyword this inside of the say hello method refers to the dog object what

52
00:03:25,680 --> 00:03:27,530
is the value of this stuff.

53
00:03:27,540 --> 00:03:32,180
First name inside of the say hello method.

54
00:03:32,610 --> 00:03:37,870
Since the dog object does not have a key of first name the value will be undefined.

55
00:03:38,160 --> 00:03:42,230
So why don't we call the say hello method by typing person dog.

56
00:03:42,270 --> 00:03:44,830
Say hello we can see it returns.

57
00:03:44,830 --> 00:03:46,850
Hello undefined.

58
00:03:47,580 --> 00:03:54,750
We can also see that our determined context function inside of the dog object is returning false because

59
00:03:54,750 --> 00:04:02,450
the keyword this inside of the determine context function does not refer to the person object anymore

60
00:04:02,470 --> 00:04:03,190
.

61
00:04:04,050 --> 00:04:07,760
So how can we fix this or if we want our Say hello method to return.

62
00:04:07,770 --> 00:04:14,070
Hello cold instead of hello undefined you would need some way of explicitly changing the value of the

63
00:04:14,080 --> 00:04:21,840
keyword this and that is exactly what the call apply and bind functions can do for us in the next video

64
00:04:21,840 --> 00:04:22,120
.

65
00:04:22,140 --> 00:04:27,990
We'll see the third rule for determining the value of the keyword this using call apply and bind.

66
00:04:28,060 --> 00:04:29,190
Things are getting exciting.

67
00:04:29,190 --> 00:04:30,340
See you in the next video.
