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,890
Now, another thing related to functions
2
00:00:04,890 --> 00:00:07,020
which you'll see quite a bit in this course
3
00:00:07,020 --> 00:00:11,310
is that you can define functions inside of functions.
4
00:00:11,310 --> 00:00:13,860
Now, when writing vanilla JavaScript code,
5
00:00:13,860 --> 00:00:17,700
this often might not make too much sense
6
00:00:17,700 --> 00:00:20,640
but it will make more sense in the context of React,
7
00:00:20,640 --> 00:00:22,560
as you will see later.
8
00:00:22,560 --> 00:00:25,440
Nonetheless, you can define a function,
9
00:00:25,440 --> 00:00:27,360
for example, called init,
10
00:00:27,360 --> 00:00:31,140
which then contains another function defined inside of it,
11
00:00:31,140 --> 00:00:32,910
for example, a function named greet
12
00:00:32,910 --> 00:00:34,593
which console logs something.
13
00:00:35,880 --> 00:00:37,710
Then, inside of init,
14
00:00:37,710 --> 00:00:39,960
you can execute greet
15
00:00:39,960 --> 00:00:43,560
but you can't execute greet outside of init.
16
00:00:43,560 --> 00:00:46,380
Because greet is now defined inside of init
17
00:00:46,380 --> 00:00:49,260
and therefore it's scoped to this init function,
18
00:00:49,260 --> 00:00:52,110
which means it's only available in there,
19
00:00:52,110 --> 00:00:56,100
just as a variable you would define in here
20
00:00:56,100 --> 00:00:58,350
would only be available inside of init
21
00:00:58,350 --> 00:00:59,523
and not outside of it.
22
00:01:00,810 --> 00:01:03,510
And therefore, inside of init you can execute it
23
00:01:03,510 --> 00:01:05,910
but outside of init you can't.
24
00:01:05,910 --> 00:01:08,010
Here though, we can execute init
25
00:01:08,010 --> 00:01:11,100
because init is scoped to the overall file
26
00:01:11,100 --> 00:01:14,583
since init is not nested into another function.
27
00:01:15,780 --> 00:01:17,910
And therefore, with this code here,
28
00:01:17,910 --> 00:01:19,770
if you reload this,
29
00:01:19,770 --> 00:01:22,140
you will see that we do output hi,
30
00:01:22,140 --> 00:01:25,020
so that this code here does get executed
31
00:01:25,020 --> 00:01:27,090
because in the end, we're executing this function,
32
00:01:27,090 --> 00:01:29,190
which then internally calls greet,
33
00:01:29,190 --> 00:01:32,703
which is this function here that was defined inside of init.
34
00:01:34,290 --> 00:01:38,220
So, you are able to define and execute functions
35
00:01:38,220 --> 00:01:40,080
inside of other functions.
36
00:01:40,080 --> 00:01:41,310
And again,
37
00:01:41,310 --> 00:01:44,970
whilst this is not something you might be doing too often
38
00:01:44,970 --> 00:01:47,250
in just JavaScript,
39
00:01:47,250 --> 00:01:49,830
it is something you'll do a lot
40
00:01:49,830 --> 00:01:51,840
as you will see throughout this course
41
00:01:51,840 --> 00:01:53,763
when writing React code.
3112
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.