JSFiddle - React, Tailwind, and code Playground
JavaScript
function addCounterToFn(fn, count = 1) {
return function () {
fn.apply(null, arguments);
return count++;
}
}
function foo() {
console.log('asd')
}
let count;
const _foo = addCounterToFn(foo);
// NOTE:
// if you want to change the count start
// add a second argument: addFnCounter(foo, 3)
// the above example will start the count from 3
count = _foo(); // 1
count = _foo(); // 2
count = _foo(); // 3
console.log(count);