-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pattern.js
48 lines (36 loc) · 907 Bytes
/
module_pattern.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
// Standart module pattern
const UICtrl = (function() {
let text = 'Hello world'
const changeText = function() {
const element = document.querySelector('.output')
element.textContent = text
}
return {
callChangeText: function() {
changeText()
console.log(text)
}
}
})();
UICtrl.callChangeText()
// Revealing module pattern
const itemCtrl = (function(){
let data = []
function addItem(item) {
data.push(item)
console.log('Item added')
}
function getItem(id) {
return data.find(item => {
return item.id === id
})
}
return {
add: addItem,
get: getItem
}
})()
itemCtrl.add({id: 1, name: 'John'})
console.log(itemCtrl.get(1))
const element = document.querySelector('.output2')
element.textContent = JSON.stringify(itemCtrl.get(1))