forked from HowProgrammingWorks/ReactiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-proxy.js
More file actions
70 lines (58 loc) · 1.37 KB
/
3-proxy.js
File metadata and controls
70 lines (58 loc) · 1.37 KB
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
'use strict';
const ROWS = 5;
const columns = ['A', 'B', 'C', 'D', 'E', 'F'];
const data = {};
const table = new Proxy(data, {
get(obj, key) {
console.log('get', key);
const cell = obj[key];
return cell ? cell.value : '';
},
set(obj, key, value) {
console.log('set', key, value);
const type = typeof value;
if (type === 'function') {
const expression = value;
value = expression();
obj[key] = { value, expression };
} else {
obj[key] = { value };
}
return true;
}
});
// Usage
table.A1 = 'city';
table.B1 = 'population';
table.C1 = 'area';
table.D1 = 'density';
table.E1 = 'country';
table.F1 = 'relative';
table.A2 = 'Shanghai';
table.B2 = 24256800;
table.C2 = 6340;
table.D2 = 3826;
table.E2 = 'China';
table.A3 = 'Beijing';
table.B3 = 21516000;
table.C3 = 16411;
table.D3 = 1311;
table.E3 = 'China';
table.A4 = 'Delhi';
table.B4 = 16787941;
table.C4 = 1484;
table.D4 = 11313;
table.E4 = 'India';
table.D5 = () => Math.max(table.D2, table.D3, table.D4);
table.F2 = () => Math.round(table.D2 * 100 / table.D5) + '%';
table.F3 = () => Math.round(table.D3 * 100 / table.D5) + '%';
table.F4 = () => Math.round(table.D4 * 100 / table.D5) + '%';
const output = [];
for (let i = 2; i <= ROWS; i++) {
const row = {};
output[i] = row;
for (const col of columns) {
row[col] = table[col + i];
}
}
console.table(output);