Created
July 16, 2022 20:46
-
-
Save Kreijstal/93b72344b26b215fb0ea5b901e6d2644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function traceMethodCalls(obj) { | |
const handler = { | |
get(target, propKey, receiver) { | |
console.log('GET', propKey); | |
const targetValue = target[propKey]; // Reflect.get(target, propKey, receiver); | |
if (typeof targetValue === 'function') { | |
return function(...args) { | |
console.log('CALL', propKey, args); | |
return targetValue.apply(target, args); // (A) | |
} | |
} | |
return targetValue; | |
}, | |
set(obj, prop, value) { | |
// The default behavior to store the value | |
obj[prop] = value; | |
console.log('SET', prop, value); | |
// Indicate success | |
return true; | |
} | |
}; | |
return new Proxy(obj, handler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment