Skip to content

Commit a06e6a3

Browse files
committed
Proxy example
1 parent 74fe437 commit a06e6a3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

JavaScript/2-proxy.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const ops = {
4+
max: Math.max,
5+
sum: (...nums) => nums.reduce((a, b) => a + b),
6+
avg: (...nums) => ops.sum(...nums) / nums.length,
7+
};
8+
9+
const lazy = (data) => new Proxy(data, {
10+
get: (data, key) => (ops[key] ? ops[key](...data) : data[key])
11+
});
12+
13+
const data = lazy([10, -17, 0, 2, 9, -4, -1, 32, 14]);
14+
15+
console.dir({
16+
max: data.max,
17+
sum: data.sum,
18+
avg: data.avg,
19+
});

0 commit comments

Comments
 (0)