function once(fn) { let flag = false; let result = null; return function (...args) { if (flag) { return result; } flag = true; result = fn.call(this, ...args); return result; }; } let onceFn1 = once((a, b) => { return a + b; }); console.log(onceFn1(1, 2)); // 3 console.log(onceFn1(3, 2)); // 3 console.log(onceFn1(3, 3)); // 3 let onceFn2 = once((a, b) => { return a - b; }); console.log(onceFn2(3, 2)); // 1 console.log(onceFn2(4, 2)); // 1