Skip to content

Instantly share code, notes, and snippets.

@semlinker
Last active October 18, 2022 08:09
Show Gist options
  • Save semlinker/914a89ab3cef6a34794f5d0fbdbcf6c0 to your computer and use it in GitHub Desktop.
Save semlinker/914a89ab3cef6a34794f5d0fbdbcf6c0 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Trace Method Call
function traceMethodCall(obj) {
const handler = {
get(target, propKey, receiver) {
const propValue = target[propKey]; // Get the original method
return typeof propValue !== "function"
? propValue
: function (...args) {
const result = propValue.apply(this, args);
console.log(
`Call ${propKey} method -> ${JSON.stringify(result)}`
);
return result;
};
},
};
return new Proxy(obj, handler);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment