[JS] Closure

Chaewon Yoon (Jamie)·2023년 5월 30일
0

[Today I learned]

목록 보기
29/32

Example 1

function createCounter() {
	let value = 0;
    
    function increment() {
    	return ++value;
    }
    
    return {
    	increment: increment
    }
}

const counter1 = createCounter();
const counter2 = createCounter();

console.log(counter1.increment())
console.log(counter1.increment())

console.log(counter2.increment())

// 1
// 2
// 1
profile
keep growing as a web developer!🧃

0개의 댓글