-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_scoping_demo.js
125 lines (104 loc) · 2.69 KB
/
6_scoping_demo.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
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
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Returns a random element from the given array.
* @param {Array} arr given array
* @returns {*} randomly returned element
*/
const getRandElem = arr => {
const randIdx = Math.floor(Math.random() * arr.length);
return arr[randIdx];
};
/**
* Returns a random character from the characters.
* This function is created from a function self-invocation, to create a
* closure.
* Feel free to check out my notes on closure.
* @returns {string} randomly returned character
*/
const getChar = (() => {
const NAMES = ['Alice', 'Bob', 'Chris', 'David', 'Ella', 'Frank', 'Gary'];
return () => {
return getRandElem(NAMES);
};
})();
const hero = getChar();
console.log(`Hero: ${hero}`);
/**
* Returns a foil, who is different from the given story hero.
* @param {string} hero story hero
* @returns {string} returned foil
*/
const getFoil = hero => {
let char = getChar();
while (char === hero) {
char = getChar();
}
return char;
};
/**
* Returns a random deed from the deeds.
* This function is created from a function self-invocation, to create a
* closure.
* Feel free to check out my notes on closure.
* @returns {string} randomly returned character
*/
const getDeed = (() => {
const DEEDS = ['Eyes', 'Stares', 'Tips', 'Tops'];
return () => {
return getRandElem(DEEDS);
};
})();
const sagas = [];
const newSaga = () => {
const foil = getChar();
console.log(`Foil: ${foil}`);
sagas.push(() => {
console.log('===== SAGA function =====');
const deed = getDeed();
console.log(`Generated deed: ${deed}`);
console.log(`${hero} ${deed} ${foil}`);
console.log('===== End =====');
});
};
console.log('First invokation...');
newSaga();
sagas[0]();
console.log();
sagas[0]();
console.log();
// Hero: Chris
// First invokation...
// Foil: Alice
// ===== SAGA function =====
// Generated deed: Eyes
// Chris Eyes Alice
// ===== End =====
// ===== SAGA function =====
// Generated deed: Stares
// Chris Stares Alice
// ===== End =====
// The function at sagas[0] REMEMBERS the outer "foil", so these two invokations
// outputs the same foil "Alice".
console.log('Second invocation...');
newSaga();
sagas[0]();
console.log();
sagas[1]();
console.log();
sagas[0]();
// Foil: Frank
// ===== SAGA function =====
// Generated deed: Tops
// Chris Tops Alice
// ===== End =====
// Still, sagas[0] REMEMBERS its same foil "Alice".
// ===== SAGA function =====
// Generated deed: Tips
// Chris Tips Frank
// ===== End =====
// However, for sagas[1], it generates a new "foil" called "Frank", and it
// REMEMBERS that new foil "Frank".
// ===== SAGA function =====
// Generated deed: Eyes
// Chris Eyes Alice
// ===== End =====
// Still, sagas[0] REMEMBERS its same foil "Alice".