자바스크립트 함수 작성법

Gwang Seub Ma·2023년 8월 2일
0
{
 const a = document.querySelector('.box')
 function clickHandler() {
 // ...
 }
 a.addEventListener('click, clickHandler);
}

const, let을 사용할 경우 위와 같이 중괄호로 묶어주면 전역변수로 사용하지 않고
범위안에서만 변수를 사용가능 함. (변수 네이밍 중복 오류를 해결하는 방법)

아래 코드도 동일.

(function(){
 const a = document.querySelector('.box')
 function clickHandler() {
 // ...
 }
 a.addEventListener('click, clickHandler);
})()
profile
http://www.hellomrma.com

0개의 댓글

Powered by GraphCDN, the GraphQL CDN