JSFiddle - React, Tailwind, and code Playground
by ruhul105
JavaScript
let i = 1;
function incrementBy(value) {
i += value;
return i;
}
export default function once(func) {
let ranOnce = false;
let value;
console.log("this",this)
return function (...args) {
if (!ranOnce) {
//value = func.apply(this, args);
value = func(args);
console.log("this",this)
console.log("this value",value)
ranOnce = true;
}
return value;
};
}
const incrementByOnce = once(incrementBy);
incrementByOnce(2); // i is now 3; The function returns 3.
incrementByOnce(3); // i is still 3; The function returns the result of the first invocation, which is 3.
i = 4;
incrementByOnce(2); // i is still 4 as it is not modified. The function returns the resul