-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-pattern.js
36 lines (30 loc) · 917 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
/* The Module Pattern is a design pattern in JavaScript that
provides a way to encapsulate and organize code by creating self-contained modules */
const calculateWeight = (function () {
const POUND = 0.220462;
const TONS = 0.000110231
const METRIC_TONS = 0.0001
let userWeight = 0
function convert(weight, type) {
userWeight = weight;
switch (type) {
case 1:
return weight * POUND
case 2:
return weight * TONS
case 3:
return weight * METRIC_TONS
default:
return weight;
}
}
return {
weight: userWeight,
converWeight: function (weight, type) {
return convert(weight, type)
}
}
})();
console.log(calculateWeight.weight)
console.log(calculateWeight.userWeight)
console.log(calculateWeight.converWeight(10, 1))