Is there any difference b/w the following 2 patterns? What is the advantage of self-executing the first one?
var testModule = (function () {
var counter = 0;
return {
incrementCounter: function () {
return counter++;
},
resetCounter: function () {
console.log( "counter value prior to reset: " + counter );
counter = 0;
}
};
})();
testModule.incrementCounter(); // 1
Next:
var testModule2 = function () {
var counter = 0;
return {
incrementCounter: function () {
return counter++;
},
resetCounter: function () {
console.log( "counter value prior to reset: " + counter );
counter = 0;
}
}
}
var result = testModule2();
result.incrementCounter(); //1