-
Notifications
You must be signed in to change notification settings - Fork 6
/
04_design_patterns.js
216 lines (198 loc) · 5.21 KB
/
04_design_patterns.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*======================================
= Module Pattern =
======================================*/
/**
* Our Module which is a IIFE saved to a variable. The object after
* return gets returned immedietly.
* @return {Object}
*/
var CountryDatabase = (function(){
/**
* Array of countries to loop through
* @type {Array}
*/
var countries = [
{
name: "Mexico",
continent: "North America",
population: 129386794,
pFemale: 0.507
},
{
name: "Canada",
continent: "North America",
population: 36446796,
pFemale: 0.504
},
{
name:"Czech Republic",
continent:"Europe",
population:10556351,
pFemale: 0.509
},
{
name: "Iceland",
continent: "Europe",
population: 332631,
pFemale: 0.496
}
];
//The whole function returns an object
//with all the functions belonging to
//our modules and all the data that it uses.
return {
getTotalPopulation: function(){
//We refer to countries which is in the scope above
//and therefore available to this function. It exists in
//a closure
return countries.reduce((totalPop, country) => {
return totalPop + country.population;
}, 0);
},
getCountryByContinent: function(continent){
return countries.filter((country) => {
return country.continent === continent;
});
},
//Sometimes it is better to create a new array
//with the new value and replace the existing
//array with the new array.
addNewCountry: function(newCountry){
var newCountriesArray = countries;
newCountriesArray.push(newCountry);
countries = newCountriesArray;
},
//We can have a "get"-method to just return the array,
//if we have a function that returns the array instead
//of directly accessing it via "CountryDatabase.countries"
//we get much more control, and we can choose to remove the
//function and therefore remove the access to the array
returnCountriesArray: function(){
return countries;
}
}
//End of return
})();
//We can only get the data from the exposed functions
//console.log(CountryDatabase.getTotalPopulation());
//This will result in an error
//console.log(CountryDatabase.countries);
/*================================================
= Revealing Module Pattern =
================================================*/
/**
* Same as above but with a bit different implementation.
* Here we can choose to hide certain functions and implementation.
* @return {Object}
*/
var RevealCountryDatabase = (function(){
var countries = [
{
name: "Mexico",
continent: "North America",
population: 129386794,
pFemale: 0.507
},
{
name: "Canada",
continent: "North America",
population: 36446796,
pFemale: 0.504
},
{
name:"Czech Republic",
continent:"Europe",
population:10556351,
pFemale: 0.509
},
{
name: "Iceland",
continent: "Europe",
population: 332631,
pFemale: 0.496
}
];
//We can put the functions in the IIFE. All functions will be
//private if we do this.
function getTotalPopulation() {
return countries.reduce((totalPop, country) => {
return totalPop + country.population;
}, 0);
}
function getCountryByContinent(continent){
return countries.filter((country) => {
return country.continent === continent;
});
}
function addNewCountry(newCountry){
var newCountriesArray = countries;
newCountriesArray.push(newCountry);
countries = newCountriesArray;
}
function returnCountriesArray() {
return countries;
}
//In the return statement we refer to the function in the outer
//scope and return in. The right hand side, the value, is the inner
//function and the left hand side is the public function. If we would
//call this function we would use RevealCountryDatabase.getArray();
return {
returnCountriesArray: returnCountriesArray
}
})();
//console.log(RevealCountryDatabase.getArray());
/*---------- ES6 Implementation ----------*/
/**
* Same as above but with ES6. Arrow Functions, let and const.
* @return {Object}
*/
const RevealCountryDatabaseES6 = (function(){
const countries = [
{
name: "Mexico",
continent: "North America",
population: 129386794,
pFemale: 0.507
},
{
name: "Canada",
continent: "North America",
population: 36446796,
pFemale: 0.504
},
{
name:"Czech Republic",
continent:"Europe",
population:10556351,
pFemale: 0.509
},
{
name: "Iceland",
continent: "Europe",
population: 332631,
pFemale: 0.496
}
];
const getTotalPopulation = () => {
return countries.reduce((totalPop, country) => {
return totalPop + country.population;
}, 0);
}
const getCountryByContinent = (continent) => {
return countries.filter((country) => {
return country.continent === continent;
});
}
const addNewCountry = (newCountry) => {
let newCountriesArray = countries;
newCountriesArray.push(newCountry);
countries = newCountriesArray;
}
const returnCountriesArray = () => {
return countries;
}
return {
getArray: returnCountriesArray
}
})();
//console.log(RevealCountryDatabaseES6.getArray());