Simple Module Pattern

Yet another counter example!

by Steven Senkus

JavaScript

var testModule = (function() {
    var counter = 0;
    var report;
    return {
        incrCounter: function(asdf) {
            report(++counter);
        },
        currentCount: function() {
            report(counter);
        },
        resetCounter: function() {
            counter = 0;
        },
    
        setReport: function(logger) {
            report = logger;
        },
        doSomeTask: function(task, taskArgs, times) {
            for (var i = 0; i < times; i++) {
                task(taskArgs[0]);
                this.incrCounter();
            }
        }
    };
})();
testModule.setReport(console.log);
testModule.incrCounter();
testModule.incrCounter();
testModule.currentCount();
// There is a better way, I am sure of it!
testModule.doSomeTask(console.log, ['run task'], 5 );