Skip to content

Instantly share code, notes, and snippets.

@Kreijstal
Created July 16, 2022 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kreijstal/93b72344b26b215fb0ea5b901e6d2644 to your computer and use it in GitHub Desktop.
Save Kreijstal/93b72344b26b215fb0ea5b901e6d2644 to your computer and use it in GitHub Desktop.
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