1// details can be found here: https://2ality.com/2017/11/proxy-method-calls.html
2
3function traceMethodCalls(obj) {
4 const handler = {
5 get(target, propKey, receiver) {
6 const targetValue = Reflect.get(target, propKey, receiver);
7 if (typeof targetValue === 'function') {
8 return function (...args) {
9 console.log('CALL', propKey, args);
10 return targetValue.apply(this, args); // (A)
11 }
12 } else {
13 return targetValue;
14 }
15 }
16 };
17 return new Proxy(obj, handler);
18}
19
20const objWithFunction = {
21 methodA: (a) => { return a; }
22}
23
24const proxyWithTrace = traceMethodCalls(objWithFunction);