-
Notifications
You must be signed in to change notification settings - Fork 0
/
modulepattern.js
55 lines (42 loc) · 1.21 KB
/
modulepattern.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
var basketModule = (function () {
// privates
var basket = [];
function getItemList() {
let itemList = [];
for(let value of basket){
itemList.push(value.item);
}
return itemList
}
function doSomethingElsePrivate() {
//...
}
// Return an object exposed to the public
return { basket,
// Add items to our basket
addItem: function( values ) {
basket.push(values);
},
// Get the count of items in the basket
getItemCount: function () {
return basket.length;
},
// Public alias to a private function
getItems: getItemList,
// Get the total value of items in the basket
getTotal: function () {
var itemCount = this.getItemCount(),
total = 0;
while (itemCount--) {
total += basket[itemCount].price;
}
return total;
}
};
}());
console.log(basketModule.getItemCount());
basketModule.addItem({ item:"Apple", price:12.0 });
basketModule.addItem({item:"Mango", price:10.0 });
console.log(basketModule.getItemCount());
console.log(basketModule.getTotal());
console.log(basketModule.getItems());