Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1 1
Let's do the opposite of the last lecture 2
2
and create a function that returns a new function. 3
3
So I'm gonna write a very simple greet function here, 4
4
because this is really just to demonstrate this point here 5
5
as easily as possible. 6
6
So here we can pass in a greeting like hi 7
7
or hello or something like that. 8
8
And then all this function will do 9
9
is to return a new function. 10
10
So as always, 11
11
we write return and then simply a new function value. 12
12
And this new function that we return 13
13
will also have a parameter so it will be able 14
14
to receive arguments. Right? 15
15
And so what this function here will do, 16
16
is it will simply look to the console, 17
17
basically the greeting, and then the name of the person 18
18
that we passed into this function. 19
19
So let's just write this 20
20
and it will make sense when we see this in action. 21
21
I'm sure it looks a bit weird right now. 22
22
And now let's actually use the greet function. 23
23
So let's say greet 24
24
with hey and so 'hey' is the greeting here. 25
25
Now what will be the result of this function call? 26
26
Well, it's gonna be this function, right? 27
27
And so let's store the result of this function call, 28
28
which will be as we just said, that function, 29
29
I'm gonna call this one greeterHey. 30
30
So this value here now is actually now a function. 31
31
Okay. And essentially it is this function. 32
32
So what this means is that we can now call 33
33
this greeter function just as if it was any other function 34
34
that we defined ourselves. 35
35
So let's say greeterHey Jonah's 36
36
and let's call it another time. 37
37
Greeter Hey Steven 38
38
and now I'm gonna run it. 39
39
And indeed we get Hey Jonas and Hey Steven. 40
40
And so this worked again because this greeterHey, 41
41
it's essentially this. 42
42
And so now we're calling it with the argument of Jonas. 43
43
And so this name here is then of course, 44
44
this name that we specified here. 45
45
Now, the greeting is still coming from 46
46
this greet function here. 47
47
And in case you're wondering why that actually works, 48
48
it is because of something called a closure. 49
49
Now closures are a very complex and difficult 50
50
to understand mechanism that's part of JavaScript. 51
51
And so there are two separate videos at the end 52
52
of the section about closures. 53
53
So closures is one of the 54
54
most misunderstood topics in JavaScript. 55
55
So don't worry about that Now, 56
56
what matters here is that our first function a greet 57
57
returned a new function that we stored into this variable. 58
58
And so this variable is now just a function that we can call 59
59
as we did here again sing the parenthesis syntax. 60
60
And of course we could also do it all in one go. 61
61
So we can again, call greet. let's say hello now. 62
62
And so again this year is now a function. 63
63
Okay so if this is a function, 64
64
we can immediately call it, right? And so we call like this, 65
65
and then we passed into name which is the argument 66
66
of this function here, right? And so even though 67
67
this looks a bit weird, it is gonna to work. 68
68
And so we get hello Jonas now.All right. 69
69
And now this example might look a bit weird 70
70
and unnecessary for you Like what's the point 71
71
of having functions returning other functions? 72
72
Well, this will actually become extremely 73
73
useful in some situations. 74
74
And especially if we're using a really important 75
75
programming paradigm called functional programming. 76
76
And I think I've mentioned it sometimes throughout 77
77
the course and we're actually gonna 78
78
look at functional programming by the end of the course. 79
79
Maybe not by the time I first published the course, 80
80
but certainly some point later. 81
81
Anyway make sure to understand this here 82
82
and especially this line of code. 83
83
And if you understand this, then I'm sure 84
84
you understood everything what happened here. 85
85
Now, just as a small challenge to finish here, 86
86
try to rewrite this function here 87
87
using only arrow functions. 88
88
And believe me that's gonna look a little bit confusing, 89
89
but I'm sure you can do it. 90
90
So just take some time and try to rewrite 91
91
that greet function using arrow functions. 92
92
And I see you here in a minute. 93
93
Okay, 94
94
so let's 95
95
see how it's done 96
96
so greet 97
97
Just gonna call it R, 98
98
so this one takes again, 99
99
greeting as an argument and it returns a new function. 100
100
So this means that it only has one line of code, 101
101
which returns something. 102
102
So all we need is this arrow. 103
103
And then we don't even need the curly braces 104
104
and we don't need to return a statement. 105
105
So what are we gonna return from here? 106
106
A new function. 107
107
So again, an arrow function and so another arrow here, 108
108
and then simply this 109
109
let's give it a safe and a try 110
110
So greet 111
111
R 112
112
and let's use hi, this time and yeah, 113
113
we get hi Jonas, 114
114
okay? 115
115
So this is an even shorter way 116
116
of writing the same function but in my opinion, 117
117
it's actually a lot more confusing. 118
118
And that's why I wrote it using 119
119
this more traditional way here. 120
120
But in the end, 121
121
it's also simply one arrow function 122
122
returning another arrow function. 123
123
And so that's the essence of this video, 124
124
which I hope was clear to you. 125
125
And if so, then let's move on right to the next one.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.