forked from tzeikob/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
43 lines (33 loc) · 865 Bytes
/
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
var myNS = myNS || Object.create(null);
myNS.Calculator = function Calculator() {
this.result = 0;
};
myNS.Calculator.prototype.add = function add(val) {
this.result += val;
return this.result;
};
myNS.Calculator.prototype.subtract = function subtract(val) {
this.result -= val;
return this.result;
};
myNS.Calculator.prototype.multiply = function multiply(val) {
this.result *= val;
return this.result;
};
myNS.Calculator.prototype.divide = function divide(val) {
this.result /= val;
return this.result;
};
myNS.Calculator.prototype.sqrt = function sqrt() {
this.result = Math.sqrt(this.result);
return this.result;
};
myNS.Calculator.prototype.clear = function clear() {
this.result = 0;
};
let calc = new myNS.Calculator();
calc.add(18);
calc.subtract(9);
calc.sqrt();
calc.multiply(calc.result);
console.log(calc.result); // 9