forked from tzeikob/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
55 lines (42 loc) · 1.15 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
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;
};
myNS.BetaCalculator = function BetaCalculator() {
myNS.Calculator.call(this);
};
myNS.BetaCalculator.prototype = Object.create(myNS.Calculator.prototype);
myNS.BetaCalculator.prototype.log = function log() {
this.result = Math.log(this.result);
return this.result;
};
let calc = new myNS.BetaCalculator();
calc.add(18);
calc.subtract(9);
calc.sqrt();
calc.multiply(calc.result);
calc.log();
console.log(calc.result); // 2.1972245773362196