Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,070 --> 00:00:04,140
And with that we're almost done
2
00:00:04,140 --> 00:00:05,550
for this section,
3
00:00:05,550 --> 00:00:09,870
but there are a couple of essential features that we'll use
4
00:00:09,870 --> 00:00:12,480
in this course and a couple of tricky features,
5
00:00:12,480 --> 00:00:16,500
which I also wanna refresh here so that you know about these
6
00:00:16,500 --> 00:00:18,510
JavaScript concepts and features,
7
00:00:18,510 --> 00:00:21,780
and you don't wonder about them when you encounter them
8
00:00:21,780 --> 00:00:24,030
later in the course.
9
00:00:24,030 --> 00:00:27,450
The first JavaScript feature you should be aware of is
10
00:00:27,450 --> 00:00:31,803
that you can pass functions as values to other functions.
11
00:00:32,970 --> 00:00:35,100
For example, we could set a timer
12
00:00:35,100 --> 00:00:38,040
with help of the built-in setTimeout function,
13
00:00:38,040 --> 00:00:40,323
a function that is provided by the browser.
14
00:00:41,340 --> 00:00:44,910
Now setTimeout wants two input values.
15
00:00:44,910 --> 00:00:47,013
It accepts two parameters.
16
00:00:48,090 --> 00:00:51,060
The first input value it wants is
17
00:00:51,060 --> 00:00:53,016
a function itself though,
18
00:00:53,016 --> 00:00:55,170
a function that can be defined
19
00:00:55,170 --> 00:00:59,040
with the function keyword or as an arrow function.
20
00:00:59,040 --> 00:01:01,680
Now, what's really important to understand here is
21
00:01:01,680 --> 00:01:06,210
that what we're doing here is we're creating a new function,
22
00:01:06,210 --> 00:01:09,390
an anonymous function here since it has no name,
23
00:01:09,390 --> 00:01:11,730
but nonetheless, a function,
24
00:01:11,730 --> 00:01:15,420
and we could absolutely create that function manually
25
00:01:15,420 --> 00:01:16,623
in a separate step.
26
00:01:18,180 --> 00:01:21,810
It could be named handleTimeout or anything like this,
27
00:01:21,810 --> 00:01:24,507
and here we could console.log ("Timed out").
28
00:01:26,610 --> 00:01:29,490
So this would now be a regular function,
29
00:01:29,490 --> 00:01:32,130
and we could also alternatively define it
30
00:01:32,130 --> 00:01:33,810
as an arrow function.
31
00:01:33,810 --> 00:01:35,433
Could be named handleTimeout2,
32
00:01:36,690 --> 00:01:39,540
and we store an arrow function in that constant.
33
00:01:39,540 --> 00:01:42,210
Because arrow functions never accept a name
34
00:01:42,210 --> 00:01:43,170
in front of them,
35
00:01:43,170 --> 00:01:46,200
they always are created in an anonymous way,
36
00:01:46,200 --> 00:01:50,130
but we can make them non-anonymous by then storing them
37
00:01:50,130 --> 00:01:53,283
in a constant or a variable that has a certain name,
38
00:01:54,900 --> 00:01:58,830
and then here I could console.log ("Timed out") again,
39
00:01:58,830 --> 00:02:00,330
anything like this.
40
00:02:00,330 --> 00:02:02,880
So here I'm creating a function with the function keyword
41
00:02:02,880 --> 00:02:06,090
or as an arrow function stored in a constant.
42
00:02:06,090 --> 00:02:08,789
And we can now also pass this function
43
00:02:08,789 --> 00:02:11,370
or this constant that contains a function
44
00:02:11,370 --> 00:02:13,830
as a value to set the timeout,
45
00:02:13,830 --> 00:02:18,060
so as a value for this first parameter that wants a function
46
00:02:18,060 --> 00:02:21,540
instead of defining the function right here in place,
47
00:02:21,540 --> 00:02:24,750
which would be an alternative though.
48
00:02:24,750 --> 00:02:27,300
Now what's really important to understand here
49
00:02:27,300 --> 00:02:30,330
when you're passing a function to another function though,
50
00:02:30,330 --> 00:02:33,450
is that when you're not defining it here in place,
51
00:02:33,450 --> 00:02:36,210
but instead you defined it in advance,
52
00:02:36,210 --> 00:02:39,240
you pass it by just using its name,
53
00:02:39,240 --> 00:02:41,250
so you don't add parentheses here.
54
00:02:41,250 --> 00:02:43,210
You omit those parentheses
55
00:02:44,340 --> 00:02:46,530
because by just using the name here
56
00:02:46,530 --> 00:02:50,580
you're passing the function as a value to this function.
57
00:02:50,580 --> 00:02:52,770
If you would add parentheses here,
58
00:02:52,770 --> 00:02:55,742
you would instead make sure that this function
59
00:02:55,742 --> 00:02:59,550
handleTimeout here gets executed immediately at the point
60
00:02:59,550 --> 00:03:02,820
of time when this timer is set, in this case here,
61
00:03:02,820 --> 00:03:07,080
and therefore it would be the return value of that function.
62
00:03:07,080 --> 00:03:10,770
If it would return any value, which this one does not,
63
00:03:10,770 --> 00:03:14,460
that would be passed as a value for this first argument,
64
00:03:14,460 --> 00:03:18,120
this first parameter, to setTimeout.
65
00:03:18,120 --> 00:03:20,940
And that, of course, would be the wrong thing to do here
66
00:03:20,940 --> 00:03:24,120
because here I don't wanna pass the return value
67
00:03:24,120 --> 00:03:26,400
of that function to setTimeout,
68
00:03:26,400 --> 00:03:28,473
but instead the function itself,
69
00:03:29,460 --> 00:03:33,810
so that internally setTimeout can execute this function
70
00:03:33,810 --> 00:03:36,270
at some point in the future.
71
00:03:36,270 --> 00:03:38,970
And that's why you must omit the parentheses here
72
00:03:38,970 --> 00:03:41,700
so that you don't execute this function right away,
73
00:03:41,700 --> 00:03:45,573
and you instead pass the function as a value to set.Timeout.
74
00:03:46,590 --> 00:03:49,140
Well, and then here, set.Timeout takes a
75
00:03:49,140 --> 00:03:52,410
second argument as well, which is now not a function,
76
00:03:52,410 --> 00:03:55,140
but instead a number which defines the number
77
00:03:55,140 --> 00:03:57,480
of milliseconds JavaScript should wait
78
00:03:57,480 --> 00:03:59,910
until it executes this function,
79
00:03:59,910 --> 00:04:02,853
which you passed as a first argument to set.Timeout.
80
00:04:04,350 --> 00:04:07,100
And I can, of course, also do that with handleTimeout2.
81
00:04:08,790 --> 00:04:10,110
Now, just to be clear,
82
00:04:10,110 --> 00:04:14,040
you can always create an anonymous function instead
83
00:04:14,040 --> 00:04:16,380
of defining a function in advance,
84
00:04:16,380 --> 00:04:18,450
and then you could, of course, also add any code
85
00:04:18,450 --> 00:04:20,313
you want to execute to that.
86
00:04:21,750 --> 00:04:24,180
But when you define such an anonymous function,
87
00:04:24,180 --> 00:04:26,640
you're also not executing it yet.
88
00:04:26,640 --> 00:04:28,830
Instead, you're also just defining it,
89
00:04:28,830 --> 00:04:32,940
and you're passing that defined function to set.Timeout.
90
00:04:32,940 --> 00:04:36,390
So you're in the end doing the same as you're doing here
91
00:04:36,390 --> 00:04:38,160
by defining it in advance
92
00:04:38,160 --> 00:04:40,980
and passing a handler to that function,
93
00:04:40,980 --> 00:04:43,590
the name of the function to setTimeout.
94
00:04:43,590 --> 00:04:45,510
You're essentially doing the same here
95
00:04:45,510 --> 00:04:48,840
when defining the function in the place where it's needed.
96
00:04:48,840 --> 00:04:51,060
You're still only defining it.
97
00:04:51,060 --> 00:04:53,343
You're not executing it immediately.
98
00:04:54,930 --> 00:04:58,230
Well, and therefore here, if you save that and you reload,
99
00:04:58,230 --> 00:04:59,760
you'll see nothing initially,
100
00:04:59,760 --> 00:05:02,250
but then once these timers expire,
101
00:05:02,250 --> 00:05:05,670
you will see the different messages they output.
102
00:05:05,670 --> 00:05:09,210
Now knowing that you can pass functions as values
103
00:05:09,210 --> 00:05:12,960
to other functions is an important first step,
104
00:05:12,960 --> 00:05:15,900
but you should not think that this is only possible
105
00:05:15,900 --> 00:05:19,740
for built-in functions like set.Timeout.
106
00:05:19,740 --> 00:05:22,360
Instead, you can also build your own function
107
00:05:23,548 --> 00:05:28,170
like greeter, for example, which accepts a greet function
108
00:05:28,170 --> 00:05:31,710
parameter so a regular parameter in the end,
109
00:05:31,710 --> 00:05:36,710
but a parameter which expects to get a function as a value.
110
00:05:37,770 --> 00:05:39,600
And then inside of greeter,
111
00:05:39,600 --> 00:05:42,930
we could execute greet function like this,
112
00:05:42,930 --> 00:05:46,863
so calling it as a function by adding these parentheses,
113
00:05:47,970 --> 00:05:51,480
and now we could call greeter like this
114
00:05:51,480 --> 00:05:54,030
and pass a function to greeter,
115
00:05:54,030 --> 00:05:56,850
like, for example, here an arrow function where
116
00:05:56,850 --> 00:05:58,287
I console.log('Hi').
117
00:06:00,600 --> 00:06:03,130
If I do that and save everything
118
00:06:04,410 --> 00:06:07,500
and reload this page and open this console,
119
00:06:07,500 --> 00:06:09,810
you will see Hi here,
120
00:06:09,810 --> 00:06:12,180
and this Hi is, of course, coming from this
121
00:06:12,180 --> 00:06:14,460
console.log message,
122
00:06:14,460 --> 00:06:17,970
so this function here is getting executed
123
00:06:17,970 --> 00:06:20,970
because we're passing it as a value
124
00:06:20,970 --> 00:06:25,560
for this greetFn parameter to the greeter function,
125
00:06:25,560 --> 00:06:28,140
and inside of that greeter function
126
00:06:28,140 --> 00:06:31,440
I'm then executing this greetFn parameter,
127
00:06:31,440 --> 00:06:34,680
so the value that's received on that parameter,
128
00:06:34,680 --> 00:06:36,840
which is this function.
129
00:06:36,840 --> 00:06:41,840
So this function down here is passed as a value to greetFn,
130
00:06:44,700 --> 00:06:47,790
and then greetFn is executed here.
131
00:06:47,790 --> 00:06:51,450
Therefore, indirectly, this function is getting executed
132
00:06:51,450 --> 00:06:56,010
there because this function down there at the bottom is the
133
00:06:56,010 --> 00:06:58,593
value received here on greetFn.
134
00:06:59,430 --> 00:07:01,170
And I just wanted to mention this here
135
00:07:01,170 --> 00:07:03,990
because it really is important to be aware
136
00:07:03,990 --> 00:07:08,850
of the fact that passing functions as values is not limited
137
00:07:08,850 --> 00:07:12,000
to built-in functions like set.Timeout,
138
00:07:12,000 --> 00:07:14,670
but can be done with all functions,
139
00:07:14,670 --> 00:07:16,530
including your own functions.
140
00:07:16,530 --> 00:07:19,983
Those can also accept functions as input.
11381
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.