-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor-new-lab3.html
More file actions
61 lines (41 loc) · 904 Bytes
/
constructor-new-lab3.html
File metadata and controls
61 lines (41 loc) · 904 Bytes
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
function Calculator() {
var methods = {
"+" : function(a,b) {
return a + b;
},
"-" : function(a,b) {
return a + b;
}
}
this.calculate = function(string) {
var splStr = string.split(' ');
return methods[splStr[1]](+splStr[0],+splStr[2]);
};
this.addMethod = function(string,func) {
methods[string] = func;
};
}
var calc = new Calculator;
alert( calc.calculate("3 + 7") ); // 10
var powerCalc = new Calculator;
powerCalc.addMethod("*", function(a, b) {
return a * b;
});
powerCalc.addMethod("/", function(a, b) {
return a / b;
});
powerCalc.addMethod("**", function(a, b) {
return Math.pow(a, b);
});
var result = powerCalc.calculate("2 ** 3");
alert( result ); // 8
</script>
</body>
</html>