en.javascript.info/1-js/06-advanced-functions/03-closure/1-counter-independent/task.md
Ilya Kantor ab9ab64bd5 up
2017-03-21 14:41:49 +03:00

31 lines
516 B
Markdown

importance: 5
---
# Are counters independent?
Here we make two counters: `counter` and `counter2` using the same `makeCounter` function.
Are they independent? What is the second counter going to show? `0,1` or `2,3` or something else?
```js
function makeCounter() {
let count = 0;
return function() {
return count++;
};
}
let counter = makeCounter();
let counter2 = makeCounter();
alert( counter() ); // 0
alert( counter() ); // 1
*!*
alert( counter2() ); // ?
alert( counter2() ); // ?
*/!*
```