💾saveCallHistory(f)

make a function/method save its call history (arguments).

JStechniquedecorator💾 saveCallHistory(f)

(decorator) make a function/method save its "call history" (arguments).

// 📁 saveCallHistory.js
const {log} = console;

// throttle decorator
function saveCallHistory(f){

    // ⭐️ save call history (arguments) here
    let history = [];

    // 🔸 wrapper.history (getter)
    Object.defineProperty(wrapper, 'history', {
        get() { return history }
    })

    return wrapper;

    // ⭐️ decorated function
    function wrapper(...args){
        history.push(args);        // save arguments first
        return f.apply(this, args);
    }
    
}

// export
module.exports = { saveCallHistory }

Last updated