Skip to content

Instantly share code, notes, and snippets.

@Dosant
Created March 5, 2017 08:58
Show Gist options
  • Save Dosant/6baa15414f38c49d099d0c12d6d02ff2 to your computer and use it in GitHub Desktop.
Save Dosant/6baa15414f38c49d099d0c12d6d02ff2 to your computer and use it in GitHub Desktop.
function makeCounter() {
var currentCount = 1;
return function() { // (**)
return currentCount++;
};
}
var counter = makeCounter(); // (*)
// каждый вызов увеличивает счётчик и возвращает результат
alert( counter() ); // 1
alert( counter() ); // 2
alert( counter() ); // 3
// создать другой счётчик, он будет независим от первого
var counter2 = makeCounter();
alert( counter2() ); // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment