niceman114
8/14/2017 - 10:49 PM

Closure Sample In Javascript

Closure Sample In Javascript

var makeCounter = function() {
    var i = 0;
    return function() { return i += 1 };
}

var c1 = makeCounter(),
    c2 = makeCounter();

c1(); // 1
c1(); // 2
c1(); // 3
c2(); // 1
c1(); // 4
c2(); // 2