VariableEnvironment / LexicalEnvironment 내부

1 ) environmentRecord - 호이스팅
2 ) outer-environmentReference - 스코프

스코프란 ?

  • 식별자에 대한 유효범위.
  • 안에서 붙 바깥으로 차례로 검색해 나가는 것을 스코프 체인이라고 하며, 이를 가능케하는 것을 바로 LexicalEnvironment 의 두번째 수집 자료인 outer-environmentReference 이다.
    ( this 랑 연관되어 있음. )
  • outer-environmentReference는 현재 호출된 함수가 선언될 당시의 LexicalEnvironment를 참조한다. ( 기억해두어야 하는 개념.!!)

실행컨텍스트는 수집자료를 총 2가지를 한다. ( 호이스팅 + 스코프 객체 )

var a = 10;

var outer = function () {
    var inner = function () {
        console.log(a); //undefined : 호이스팅을 생각하면 될듯! 함수 내에 매개변수로 받은것은 없지만, 
        a라는 식별자를 받았기 때문에 스코프체인을 하지 않고 undefined를 출력한 것.
        var a = 20;
    };

    inner();
    console.log(a)  // 10 출력  : outer 함수내에서는 a 변수가 없어서 undefined가 나올거라 생각했지만,
    outer 변수의 LexicalEnvironment는 window 객체 이기 때문에 10이 출력된다.
}

console.log(a);

----------------------------------------------

var a = 10;

var outer = function () {
    var b= 1004 ;
    var inner = function () {
        console.log(b); //1004        
    }

    inner();
    console.log(a)  // 10 
}

console.log(a);

실행컨텍스트는 실행할 코드에 제공할 환경 정보들을 모아놓는 3가지 객체입니다.

1 ) VariableEnvironment
2 ) LexicalEnvironment
3 ) ThisBinding

profile
한줄한줄.

0개의 댓글