All language subtitles for 13. More Closure Examples

ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

1 1

Let's now create two more situations 2

2

in which closures are gonna appear. 3

3

So that you can start identifying closures 4

4

in your own code in the future. 5

5

And both of these examples are gonna demonstrate 6

6

that we don't need to return if function 7

7

from another function in order to create a closure. 8

8

All right, so let's start with our first example here. 9

9

I'm gonna start by defining an empty variable called f 10

10

and then a function expression g. 11

11

So these are just some generic names 12

12

because what the functions do here don't really matter. 13

13

Then in here, I defined this a variable 14

14

and then I'm going to reassign the F variable 15

15

that we created out here. 16

16

And I'm gonna assign it a function value. 17

17

And this function will then simply log 18

18

the a variable to the console 19

19

let's say times two. 20

20

Okay, and that's it for now at least. 21

21

So let's try to call g 22

22

so that's gonna be dispersed function. 23

23

And so the result of this function is that a will be 23 24

24

and that F variable that we have out here 25

25

will become this function. 26

26

And so after g we can then call F. 27

27

So let's try that. 28

28

And we get 46 which is indeed 23 times two, all right. 29

29

And so what this proves is that this F value here 30

30

so this F function really does close over any variables 31

31

of the execution context in which it was defined. 32

32

And that is true even when the variable itself. 33

33

So F here was technically not even defined inside 34

34

of this variable environment here, right. 35

35

So this F variable was defined outside here 36

36

in the global scope, it was created here 37

37

but then as we assigned it a function here 38

38

in this the g function, 39

39

so right here at instill closed 40

40

over the variable environment of the g function. 41

41

And that includes this a variable. 42

42

And therefore it is able to access this a variable here 43

43

even after the g function here at this point 44

44

has of course already finished its execution, right. 45

45

So that's just what we learned in the last lecture. 46

46

So at this point of the execution, 47

47

the variable environment of g is no longer there, right. 48

48

But f, so this function here closed over 49

49

that variable environment and therefore it is able 50

50

to access the a variable. 51

51

So basically using the analogy of or less video, 52

52

the a variable is inside the backpack of the f function. 53

53

But now let's take it to the next level 54

54

and create a new function here. 55

55

So that's h and it's gonna be very similar. 56

56

All it's gonna do is to define b as some other value, 57

57

let's say this and then we will again, 58

58

reassign f, right here. 59

59

So let's call this one a b now of course. 60

60

So this one here will then try to access b 61

61

and multiply by two. 62

62

Okay, and so what I want to see with this 63

63

is what happens when we assign the f value 64

64

a second function. 65

65

Okay, so here we call g and so g will then assign to 66

66

or empty variable this f function. 67

67

Okay, and then if we call h afterwards 68

68

or actually let's do it after calling f for the first time. 69

69

So as we then call h then the f variable 70

70

will be assigned again. 71

71

So then a second function which is this one here. 72

72

And so what I want to see is what f does then. 73

73

So let's see. 74

74

And again, keeping in mind that F at this point 75

75

is a different function than this one here 76

76

because it was reassigned by h. 77

77

We just write that here. 78

78

Okay, and now we get 1554 which is probably 777 times two 79

79

and indeed it is. 80

80

And so this proves that the f function 81

81

it was reassigned here 82

82

also closed over the variable environment of h. 83

83

And so that's how it can access then the b variable here 84

84

which has set to 777. 85

85

So let's actually inspect the variable environment like 86

86

we did by the end of the last video. 87

87

So of f and then we will be able to see that in the closure, 88

88

it does indeed have the value of b 89

89

and it now no longer has the value of a. 90

90

So after the closure that it used to have before. 91

91

So at this point here at the closure contained 92

92

the value a and we can of course also see that. 93

93

So at this point in time, the closure is indeed a 23, right. 94

94

And so then as we reassign the function to a new value, 95

95

then that old closure basically disappears 96

96

and now the closure is b. 97

97

So this variable here of the birthplace 98

98

where it was reassigned. 99

99

And this is really fascinating in my opinion 100

100

that the closure can change like this 101

101

as the variable is reassigned. 102

102

So it really is true that a closure always makes sure 103

103

that a function does not lose the connection 104

104

to the variables that were present at its birthplace. 105

105

So it's always gonna remember them. 106

106

In our case here, the function 107

107

was kind of born inside of g first 108

108

and then it was essentially reborn again in h. 109

109

And so first the closure contained the a variable 110

110

of its first birthplace. 111

111

And then as it was reborn to follow our analogy 112

112

then it remembered this B variable off its birthplace. 113

113

Okay, so this was example one. 114

114

Let's now create another example. 115

115

Okay, so I hope the first one was clear here. 116

116

So whenever something like this happens 117

117

where your reassigned functions even without returning them, 118

118

keep in mind that also this will create a closure 119

119

but now moving on to our second example here 120

120

and that's gonna be a timer. 121

121

So a timer is another great example 122

122

that we don't need to return a function 123

123

to see a closure in action. 124

124

So here I'm going back to the example of the airline. 125

125

So creating a function called board passengers 126

126

and then we get the number of passengers and a wait time. 127

127

Then let's create a new variable in here 128

128

which I'm calling perGroup. 129

129

And that's because boarding usually happens in groups 130

130

and we usually have three groups. 131

131

So I'm gonna divide all the passengers. 132

132

So the number of passengers that is boarding by three 133

133

and by the end of this function, 134

134

I want to lock to the console. 135

135

We'll start boarding and then wait seconds, okay. 136

136

And now let's actually use a timer 137

137

and we haven't actually used a timer yet in this course. 138

138

And we will learn more about them later 139

139

but this is such a good use case 140

140

that I wanted to use a timer now. 141

141

And it's actually very simple. 142

142

So we just use the set time out function 143

143

and a set time out function needs two parameters. 144

144

The first one is a function which will be executed, okay. 145

145

And this function will be executed after a certain time. 146

146

For example, we can specify a 1,000 milliseconds 147

147

because the second argument here is milliseconds. 148

148

And so that will mean that whatever code 149

149

is inside of this function will be executed 150

150

after one second. 151

151

Let me actually do that out here 152

152

just so you see this as an example. 153

153

So if I saved us now wait one second 154

154

and then here you see timer appearing. 155

155

So I'm gonna do that again. 156

156

Okay, so you'll see that after this one second, 157

157

this function was executed. 158

158

Okay, and so this, as we already learned before 159

159

is essentially a callback function. 160

160

And in this case it is literally called later. 161

161

So in this case, after one second. 162

162

So let's create or callback function here. 163

163

And first I want to log to the console. 164

164

We are now boarding all and then the n passengers. 165

165

And so this n here is this parameter of the function. 166

166

So of this parent function which essentially is the function 167

167

or the scope in which this callback function here 168

168

is being created. 169

169

And so that will of course be important 170

170

for our closure example. 171

171

And then let's also use the perGroup variable now. 172

172

There are three groups each with 173

173

passengers, okay, 174

174

that's our function and now let's call it. 175

175

And here we want to specify wait and then times 1,000 176

176

because we're gonna pass into wait in seconds. 177

177

And this argument here needs to be in milliseconds 178

178

and so we just multiply by 1,000. 179

179

So board passengers, let's say we have 180 passengers 180

180

and it takes two, let's say three seconds. 181

181

So immediately when we call this function, 182

182

this variable will be created 183

183

then set time out will be called 184

184

and it will basically register this callback function here. 185

185

And then that will be called after three seconds. 186

186

But immediately also this console.log here 187

187

will be called, all right. 188

188

So this console.log here will not wait these three seconds 189

189

for these set time out callback to finish. 190

190

Okay, so immediately this variable is created, 191

191

this set timeout function is run and console.log is run. 192

192

And then after three seconds, this function here 193

193

will be executed. 194

194

And so let's see what happens to the n variable 195

195

and to the perGroup variable that is in this function. 196

196

So immediately we got this log and then after three seconds, 197

197

we are boarding all 180 and there are three groups 198

198

with 60 passengers each. 199

199

Okay, it worked. 200

200

And again, keep in mind that this callback function here 201

201

was executed completely independently 202

202

from the board passengers function. 203

203

All right, but still the callback function 204

204

was able to use all the variables that were 205

205

in the variable environment in which it was created. 206

206

So that's n and perGroup. 207

207

And one more time, this is a clear sign 208

208

of a closure being created, right. 209

209

So the only way in which this callback function here 210

210

can have access to the variables that are defined 211

211

in the board passengers function 212

212

that has long finished execution is if it created a closure. 213

213

And so that's exactly what happened. 214

214

And yeah, that's how we got access to perGroup 215

215

and also this argument of the function 216

216

so this wait here, right. 217

217

So as I mentioned in the last lecture, 218

218

the closure of course also includes the arguments 219

219

and that's because they are really just 220

220

a local variables in the function. 221

221

And to finish, let's now also prove 222

222

that the closure does in fact have priority 223

223

over the sculpt chain. 224

224

So here in the global scope, 225

225

I'm also gonna create a variable called perGroup equals 226

226

and then just some value here. 227

227

And so if the scope chain had priority over the closure, 228

228

then this callback function here 229

229

would indeed use this variable here 230

230

this global variable 231

231

because we can imagine this function here 232

232

basically being executed in the global scope, okay. 233

233

So if it wasn't for the closure it would use this. 234

234

So let me actually demonstrate that to you. 235

235

So if I remove this variable, 236

236

then it will be able to use the perGroup 237

237

and data is here outside. 238

238

So indeed now we get 1,000 239

239

but then as we put it back here 240

240

then the callback function will close over this variable. 241

241

So it will close over 242

242

the entire variable environment in fact. 243

243

And so therefore it will then use this year first, right. 244

244

And it did. 245

245

So in fact, a disclosure even has priority 246

246

over the sculpt chain. 247

247

Okay, and with this, 248

248

we finished these two lectures about closures 249

249

and they hope that after this one, 250

250

you are now a little bit better able to identify closures 251

251

as they happen in your code or even here in my code 252

252

throughout the codes 253

253

because we will see some closures happening 254

254

some more times in the future. 255

255

Now all there's left to do is the coding challenge 256

256

in the next video where you will be thinking 257

257

about closures one more time.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.