Given this object
const myObj = {
multiply(x, y) {
return x * y;
},
squared(x) {
return x ** x;
}
};
and this method
function handleMethodCall(fnName, fnArgs) {
console.log(`${fnName} called with `, fnArgs);
}
implement this function
function interceptMethodCalls(obj, fn) {
// TODO
}
so that
interceptMethodCalls(myObj, handleMethodCall);
myObj.multiply(2, 7);
myObj.squared(2);
logs to the console
// multiply called with [ 2, 7 ]
// squared called with [ 2 ]
If you need to, you can change more than just the interceptMethodCalls
function. I've got two solutions and they slightly differ in how the interceptMethodCalls
is invoked. But the parameters should stay the same.