Ничего не понял, но накидал такое вот.
console.clear();
function saveCalls(func) {
const calls = [];
function proxy(...args) {
var ctx = this;
calls.push({
ctx: ctx,
args: args,
});
return func.apply(ctx, args);
}
proxy.calls = calls;
return proxy;
}
function sum(a, b) {
return a + b;
}
const saveSum = saveCalls(sum);
console.log(saveSum(5, 10));
console.log(saveSum(3, 1));
console.log(saveSum.calls);
const user = {
name: 'john',
say() {
return this.name;
},
};
const saveSay = saveCalls(user.say);
console.log(saveSay.call(user));
console.log(saveSay.calls);