I want to intercept function calls and found that proxies can do that
Far too complicated. All you need is a wrapper function:
// Trapping a function call with another function
const taggedTemplate = (...argArray) => {
console.log("apply");
return target(...argArray);
};
The proxy doesn't achieve anything else here.
I quickly found that the params passed to the function are not intercepted. Why is that?
Because only taggedTemplate
is a proxy (or wrapped function), and the apply
trap triggers when that particular taggedTemplate
function object is called. There is no proxy involved in Num()
. The expression taggedTemplate`my fav number is ${Num()}`
is no different than doing
const value = Num();
taggedTemplate`my fav number is ${value}`
Is there any way to do that?
No.