this는 실행 시 소유자 확인이 안되면 전역객체(window)를 가리킴
addEventListener()
function app(){
console.log(“A”)
}
document.addEventListener(‘DOMContentLoaded’, app)
//html의 모든 컨텐츠가 로딩이 끝나면 최초 app 함수 실행
this 체크
function app(){
console.log(this) //여기서 this는 document를 가리킴
const btnSave = document.querySelector('#btn-save');
const message = document.querySelector('.message');
btnSave.addEventListener('click', function(){
console.log(this)
//여기서 this는 btnSave를 카리킴 (addEventListener에서 바인딩 시켜주어서)
message.innerText ='저장되었습니다';
})
}
document.addEventListener('DOMContentLoaded', app);
//app함수의 주인이 this이지만 addEventListener에서 this를 document에 바인딩 시켜줌!