๐Ÿ’พsaveCallHistory(f)

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

JS โŸฉ technique โŸฉ decorator โŸฉ ๐Ÿ’พ 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