I'm looking at a JavaScript exercise and the output's a little confusing.
In the code below each time 'counter' is called the value of 'i' outputted to the console increases, but in the constructor 'i' is reset to '0' at the beginning of the object's code, so should really return '1' each time 'counter' is called.
Why is 'i' not being reset to '0' each time 'counter' is called?
Thanks in advance!:)
function makeCounter() {
var i = 0;
return function() {
console.log( ++i );
};
}
var counter = makeCounter();
counter(); //Output is 1.
counter(); //Output is 2.
counter(); //Output is 3...