개념글 링크

https://gall.dcinside.com/board/view/?id=programming&no=1421716&_rk=eYK&exception_mode=recommend&page=1


function(){

  for(var i=0; i < 10; i++) {

    setTimeout(function() {

      console.log(i);

    }

  }

}


클로저 정의(출처: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures)

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.


setTimeout에 전달된 콜백함수에 쓰인 i가 익명함수의 반복문에서 정의된 변수 i를 참조하고 잇으니 클로저 맞는데

근데 var가 함수 스코프라 콘솔에 9밖에 안찍히는 거잖아


클로저 + var 키워드 + 비동기 코드의 환상적인 조합인건데 왜 클로저가 안쓰였다고 하는거지?