-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path10.objectExpression.js
124 lines (104 loc) · 2.45 KB
/
10.objectExpression.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
var VARIABLES = {
'x': 0,
'y': 1,
'z': 2,
};
function Const(x) {
this.getValue = function() {
return x;
}
}
Const.prototype.toString = function() {
return this.getValue().toString();
}
Const.prototype.evaluate = function() {
return this.getValue();
}
function Variable(name) {
var num = VARIABLES[name];
this.getName = function() {
return name;
}
this.getNum = function() {
return num;
}
}
Variable.prototype.toString = function() {
return this.getName();
}
Variable.prototype.evaluate = function() {
return arguments[this.getNum()];
}
function Operation() {
var operands = Array.prototype.slice.call(arguments);
this.getOperands = function() {
return operands;
}
}
Operation.prototype.toString = function() {
return this.getOperands().join(" ") + " " + this.getSymbol();
}
Operation.prototype.evaluate = function() {
var operationArgs = arguments;
var result = this.getOperands().map(function(value) {
return value.evaluate.apply(value, operationArgs)
});
return this.count.apply(this, result);
}
function NewOperation(count, symbol) {
this.count = count;
this.getSymbol = function() {
return symbol;
}
}
NewOperation.prototype = Operation.prototype;
function makeNewOperation(count, symbol) {
var result = function() {
Operation.apply(this, arguments);
}
result.prototype = new NewOperation(count, symbol);
return result;
}
var Add = makeNewOperation(
function(a, b) {
return a + b;
},
'+'
);
var Subtract = makeNewOperation(
function(a, b) {
return a - b;
},
'-'
);
var Multiply = makeNewOperation(
function(a, b) {
return a * b;
},
'*'
);
var Divide = makeNewOperation(
function(a, b) {
return a / b;
},
'/'
);
var Negate = makeNewOperation(
function(a) {
return -a;
},
"negate"
);
var Square = makeNewOperation(
function(a) {
return a * a;
},
"square"
);
var Sqrt = makeNewOperation(
function(a) {
return Math.sqrt(Math.abs(a));
},
"sqrt"
);