Here is my self-executing function:
var incrementInt = (function() {
var manyOut = 10;
if(incInt) {
incInt+=manyOut;
} else {
var incInt = 0;
}
return {
s: incInt,
m: manyOut
};
})();
I have privatized the variables incInt and manyOut, and tied the variable incrementInt to the returned object.
My goal is to create a function that returns an object where one property is an integer that increments every time that it is called. I would like the incrementing variable to have the most narrow scope possible.
My problem with the solution I have above is that the variable incInt is re-initialized every time the function is called. Due to its scope within the function, the variable is destroyed automatically.
You’ll need to use methods to add and get the variable
incInt.