I'm wondering why this code doesn't work,
var uniqueInteger = function() {
var counter = 0;
return function() { return counter++; }
};
console.log(uniqueInteger()()); // 0
console.log(uniqueInteger()()); // 0
console.log(uniqueInteger()()); // 0
console.log(uniqueInteger()()); // 0
and this code does. The only difference is making the function self invoked instead of invoking it in console.log()
var uniqueInteger = (function() {
var counter = 0;
return function() { return counter++; }
}());
console.log(uniqueInteger()); // 0
console.log(uniqueInteger()); // 1
console.log(uniqueInteger()); // 2
console.log(uniqueInteger()); // 3
I'm very new to JS so please excuse my noobness. Thanks!