forked from tzeikob/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
66 lines (52 loc) · 1.01 KB
/
calculator.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
var myNS = myNS || Object.create(null);
myNS.calculator = function calculator() {
let result = 0;
const reset = function reset(val) {
result = val;
};
const add = function add(val) {
result += val;
return result;
};
const subtract = function subtract(val) {
result -= val;
return result;
};
const multiply = function multiply(val) {
result *= val;
return result;
};
const divide = function divide(val) {
result /= val;
return result;
};
const sqrt = function sqrt() {
result = Math.sqrt(result);
return result;
};
const clear = function clear() {
reset(0);
};
return {
add,
subtract,
multiply,
divide,
sqrt,
clear
};
}();
myNS.adder = function adder() {
const push = function push(val) {
return myNS.calculator.add(val);
};
return {
push
};
}();
let total = 0;
total = myNS.adder.push(1);
total = myNS.adder.push(1);
total = myNS.adder.push(1);
total = myNS.adder.push(1);
console.log(total); // 4