JS
클로저 문제 정답
영프로95
2022. 8. 7. 16:15
클로저 문제
<!DOCTYPE html> Document Click me! 클로저 개념을 활용하여 "Click me!" 버튼을 2번이상 클릭시 0으로 초기화 되게끔 작성해주세요.
youngpro95.tistory.com
정답은
const button = document.querySelector('button')
button.addEventListener('click', (function(){
let count =0;
return function(){
count += 1;
if(count ===2){
alert("0으로 초기화 됩니다.")
count = 0;
}
}
})());
button.addEventListener('click',counter())
function counter(){
let count = 0;
return function(){
if(count===2){
alert("0으로 초기화 됩니다.")
count = 0;
}
count++;
}
}
const increaseCount = function(){
let count = 0;
return function(){
if(count===2){
alert("0으로 초기화 됩니다.")
count = 0;
}
count++;
}
}
button.addEventListener('click', increaseCount())
1. 즉시실행함수를 사용한 방법
2. 함수선언식 방법
3. 함수표현식 방법