-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclosure.js
More file actions
113 lines (100 loc) · 2.59 KB
/
closure.js
File metadata and controls
113 lines (100 loc) · 2.59 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function familyTreeFunction() {
const grandpa = "grandpa";
return function () {
const father = "father";
return function () {
const son = "son";
console.log(`Function: ${grandpa} > ${father} > ${son}`);
};
};
}
const familyTreeArrowFunction = () => {
const grandpa = "grandpa";
return () => {
const father = "father";
return () => {
const son = "son";
console.log(`Arrow Function: ${grandpa} > ${father} > ${son}`);
};
};
};
familyTreeFunction()()();
familyTreeArrowFunction()()();
function greetingFunction(message) {
return function (firstName) {
return function (lastName) {
console.log(`${message} ${firstName} ${lastName}`);
};
};
}
const greetingArrowFunction = (message) => (firstName) => (lastName) =>
console.log(`${message} ${firstName} ${lastName}`);
greetingFunction("Hi")("Jan")("Tan");
greetingArrowFunction("Hi")("Jan")("Tan");
// Memory efficient
function memoryEfficient() {
const bigArray = new Array(7000).fill("😄");
console.log("Created once");
return function (index) {
return bigArray[index];
};
}
const getBigArray = memoryEfficient();
console.log(getBigArray(699));
console.log(getBigArray(699));
console.log(getBigArray(699));
// Encapsulation
function encapsulation(initialCount = 0, step = 1) {
let privateCount = initialCount;
function increment() {
privateCount = privateCount + step;
}
function decrement() {
privateCount = privateCount - step;
}
function getCount() {
return privateCount;
}
return {
increment,
decrement,
getCount,
};
}
const counter = encapsulation();
console.log(counter.getCount());
counter.increment();
console.log(counter.getCount());
counter.decrement();
console.log(counter.getCount());
function useState(initialState) {
let state = initialState;
return [
// why is the state value a function? No re-render in vanilla JavaScript like in React.
// if you just use the value (no function), then change it with the setter function(setState) and then the log value, it will reference a "stale" value (stale closure) -> the initial value not the changed value
() => state,
(newState) => {
state = newState;
},
];
}
const [name, setName] = useState("");
console.log(`name: ${name()}`);
setName("Tom");
console.log(`name: ${name()}`);
// Memoization
function memorizedAddTo80() {
const cache = {};
return function (n) {
if (n in cache) {
return cache[n];
} else {
cache[n] = n + 80;
return cache[n];
}
};
}
const memorized = memorizedAddTo80();
memorized(5);
memorized(5);
memorized(10);