98. 클로져
function outer() {
const x = 1;
function inner() {
return x;
}
return inner;
}
const inner = outer();
console.log(inner()); // 1활용 예제
Last updated
function outer() {
const x = 1;
function inner() {
return x;
}
return inner;
}
const inner = outer();
console.log(inner()); // 1Last updated
const counter = () => {
let privateCountValue = 0;
function changeBy(val) {
privateCountValue += val;
}
return {
increment() {
changeBy(1);
},
decrement() {
changeBy(-1);
},
value() {
return privateCountValue;
},
};
};
const c = counter();
console.log(c.value()); // 0
c.increment();
console.log(c.value()); // 1
c.increment();
console.log(c.value()); // 2
c.decrement();
console.log(c.value()); // 1