Last active
October 18, 2022 08:09
-
-
Save semlinker/914a89ab3cef6a34794f5d0fbdbcf6c0 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Trace Method Call
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 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