Module1: How Closures Work
parent
c11b998681
commit
c38c66a446
15
app.js
15
app.js
|
@ -1,5 +1,12 @@
|
||||||
let message = 'Hello';
|
function setupCounter(val) {
|
||||||
if (message === 'Hello') {
|
return function counter() {
|
||||||
var count = 100; // var is within global scope, when it's not within object or function
|
return val++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
console.log(count); // No reference error, which is dangerous
|
|
||||||
|
let counter1 = setupCounter(0);
|
||||||
|
console.log(counter1());
|
||||||
|
console.log(counter1());
|
||||||
|
let counter2 = setupCounter(10);
|
||||||
|
console.log(counter2());
|
||||||
|
console.log(counter2());
|
Loading…
Reference in New Issue