클로저, Closure
함수형 언어에서 볼 수 있는 특징이다.
시간 차에 따라 외부 지역 변수가 소멸 후 내부 함수 호출 발생할 때,
외부 지역 변수를 나중에 사용하기 위해 별도의 공간에 저장을 한다.
<script>
const btn1 = document.getElementById('btn1');
const btn2 = document.getElementById('btn2');
let num1 = 100; //전역 변수
btn1.onclick = function() {
let num2 = 200; //지역변수
btn2.onclick = function () {
// alert(num1);
alert(num2); //지역 변수 > num2의 생명주기를 생각하면 이상하다
};
};
</script>
원래대로라면 num2를 호출했을 때, not fined 에러 떠야한다.
btn2는 언제 실행될지 모르기때문에
num2가 되살아남는다.