What type of function is this? Is it like annonymous funtion that holds other functions including return statement that returns object like key’s with assigned functions that do certain operations? I found this code somewhere but it’s my first time seeing something like this. Can someone explain this type of funciton/code? Thanks.
var counter = (function() {
var myCounter = 0;
function changeBy(val) {
myCounter += val;
}
return {
increment: function () {
changeBy(1);
},
decrement: function() {
changeBy(-1);
},
value: function() {
return myCounter;
}
};
})();
console.log(counter.value());
counter.increment();
counter.increment();
console.log(counter.value());
counter.decrement();
console.log(counter.value());
counter.changeBy(2);
console.log(counter.value());