1
00:00:01,125 --> 00:00:04,875
will hold this empty object. So now that

2
00:00:04,875 --> 00:00:06,875
we have a good understanding of how the

3
00:00:06,875 --> 00:00:10,125
this object gets its value, let's look at

4
00:00:10,125 --> 00:00:12,840
the call method. In this example, we have

5
00:00:12,840 --> 00:00:15,348
two objects with properties name and age

6
00:00:15,348 --> 00:00:19,125
assigned to the variables person1 and

7
00:00:19,125 --> 00:00:21,554
person2. Next, we have a function

8
00:00:21,554 --> 00:00:23,325
expression sayHi that prints the string

9
00:00:23,325 --> 00:00:26,625
'Hi, ' followed by the values stored in

10
00:00:26,625 --> 00:00:29,489
the this.name. We know that calling the

11
00:00:29,489 --> 00:00:31,875
function sayHi just prints 'Hi, ' as this

12
00:00:31,875 --> 00:00:34,411
refers to the global Window object, which

13
00:00:34,411 --> 00:00:36,625
does not have a name property. What we

14
00:00:36,625 --> 00:00:38,875
really want here is to set the value of

15
00:00:38,875 --> 00:00:41,375
this to something other than the current

16
00:00:41,375 --> 00:00:42,875
execution context. Every JavaScript

17
00:00:42,875 --> 00:00:45,459
function object has a few properties that

18
00:00:45,459 --> 00:00:47,875
come out of the box. One of these

19
00:00:47,875 --> 00:00:50,125
properties is the call method, and you can

20
00:00:50,125 --> 00:00:52,625
pass another object as an argument to this

21
00:00:52,625 --> 00:00:56,125
call method as can be seen right here.

22
00:00:56,125 --> 00:00:57,875
Passing the person1 object to the call

23
00:00:57,875 --> 00:01:00,459
method as an argument of sayHi binds the

24
00:01:00,459 --> 00:01:03,625
value of this in sayHi to the person

25
00:01:03,625 --> 00:01:05,681
object. And this time, the string Hi, John

26
00:01:05,681 --> 00:01:09,250
gets printed to the console. In the same

27
00:01:09,250 --> 00:01:12,459
way, when we pass person2, the value Hi,

28
00:01:12,459 --> 00:01:15,925
Mary is printed to the console. You can

29
00:01:15,925 --> 00:01:18,725
also pass additional arguments to the call

30
00:01:18,725 --> 00:01:20,840
method. Here, we are passing a message

31
00:01:20,840 --> 00:01:23,625
variable in addition to the person1

32
00:01:23,625 --> 00:01:25,840
object. Call lets you run the function

33
00:01:25,840 --> 00:01:29,625
sayHi in the context of the first argument

34
00:01:29,625 --> 00:01:32,750
message. Let's see this in action in our

35
00:01:32,750 --> 00:01:35,459
demo. In this example, as expected, we get

36
00:01:35,459 --> 00:01:38,125
Hi, John and Hi, Mary in the console.

37
00:01:38,125 --> 00:01:40,792
Let's comment both our call methods and

38
00:01:40,792 --> 00:01:44,570
just run the sayHi function. Save the

39
00:01:44,570 --> 00:01:46,697
file, and the string Hi, is printed to the

40
00:01:46,697 --> 00:01:48,697
console as the Window object doesn't

41
00:01:48,697 --> 00:01:52,125
really have a property called name.

42
00:01:52,125 --> 00:01:55,125
Another thing to note here is that calling

43
00:01:55,125 --> 00:01:56,792
the sayHi.call method without any

44
00:01:56,792 --> 00:01:59,750
arguments is the same as calling the sayHi

45
00:01:59,750 --> 00:02:01,625
function. Also, you can only invoke the

46
00:02:01,625 --> 00:02:06,000
call method on another function. Calling person1.call will result in a type error

