728x90
반응형

 

init( ) 함수 : 여러 함수의 초기화를 위한 즉시실행 함수

사용이유는 ① 가독성 측면 ② 전역객체보호

 

사용예시)

const title = document.querySelector("#title");

const BASE_COLOR = "rgb(52, 73, 94)";
const OTHER_COLOR = "#7f8c8d";

function handleClick() {
const currentColor = title.style.color;
if (currentColor === BASE_COLOR) {
title.style.color = OTHER_COLOR;
} else {
title.style.color = BASE_COLOR;
}
}

function init() {
title.style.color = BASE_COLOR;
tile.addEventListener("click", handleClick);
}

init();

 

 

 

728x90
반응형