-
Notifications
You must be signed in to change notification settings - Fork 3
/
closures.js
63 lines (51 loc) · 1.64 KB
/
closures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// --------------------------
// Closures (primitive types)
// --------------------------
function outerFunction1(frozenNumber) {
function innerFunction1(variableNumber) {
// Question: Which output will be logged?
console.log(variableNumber + frozenNumber);
}
return innerFunction1;
}
// freeze the number 42
let myNumber = 42;
const myClosure1 = outerFunction1(myNumber); // which ugly number do you want to freeze?
// Mutate number
myNumber = 5;
myClosure1(4); // Which output will be logged when calling this function?
// ----------------------------
// Closures (complex objects)
// ----------------------------
function outerFunction2(frozenObject) {
function innerFunction2(variableObject) {
// Question: Which output will be logged?
console.log(variableObject.count + frozenObject.count);
}
return innerFunction2;
}
const input2 = { count: 3 };
// freeze object
const myClosure2 = outerFunction2(input2);
// mutate count
input2.count = 7;
// call closure
myClosure2({ count: 5 }); // Which output will be logged when calling this function?
// -----------------------------
// Closures (complex objects 2)
// -----------------------------
function outerFunction3(frozenObject) {
const frozenObjectCount = frozenObject.count;
function innerFunction3(variableObject) {
// Question: Which output will be logged?
console.log(variableObject.count + frozenObjectCount);
}
return innerFunction3;
}
const input3 = { count: 3 };
// freeze object
const myClosure3 = outerFunction3(input3);
// mutate count
input3.count = 7;
// call closure
myClosure3({ count: 5 }); // Which output will be logged when calling this function?