Object Closures in JavaScript

The counter function creates a scope for the n variable making it 'private' for the counter function. It returns an object with two methods that can be called using myCounter.count() and myCounter.reset() which both use the private n variable.

function counter() {
    var n = 0;
    return {
        count: function() { return ++n },
        reset: function() { n = 0; }
    };
};

var myCounter = counter();

var myNumA = myCounter.count() // 1
var myNumB = myCounter.count() // 2
myCounter.reset()
var myNumC = myCounter.count() // 1 - because counter was reset

For a more detailed article on closures take a look at the MDN website.