SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
JavaScript
 Hoisting

             안오균
먼저,
지역 변수의 유효 범위는 블럭이 아닌 함수!

 function foo() {
   var bar = ‘bar’;
   console.log(bar); //--> ‘bar’
   if (true) {
     var bar = ‘baz’;
     console.log(bar); //--> ‘baz’
   }
   console.log(bar); //--> ‘baz’
 }
즉시 실행 함수로 유효 범위를 제한할 수 있음.

 function foo() {
   var bar = ‘bar’;
   console.log(bar); //--> ‘bar’
   if (true) {
     (function () {
       var bar = ‘baz’;
       console.log(bar); //--> ‘baz’
     }());
   }
   console.log(bar); //--> ‘bar’
 }
호이스팅(Hoisting)

 변수 선언과 함수 선언은 인터프리터에 의해
 함수의 맨 앞으로 끌어올려진다(옮겨진다).

 * hoist = 끌어올리다
용어가 좀 헷갈릴 수 있다.

변수 선언(Variable Declaration)
 var foo = ‘bar’;


함수 선언(Function Declaration)
 function foo() {}


함수 표현식(Function Expression)
 var foo = function () {};
 (function foo() {});
변수 선언인 경우

 function foo() {
   console.log(bar); //--> undefined
   var bar = 'baz';
   console.log(bar); //--> 'baz'
 }

아래와 동일
 function foo() {
   var bar;
   // 변수 선언은 함수의 맨 앞으로 끌어올려진다.(hoisted)
   console.log(bar);
   bar = 'baz'; // 할당은 실행 시점에 수행
   console.log(bar);
 }
for 문의 경우도 동일

 function foo(arr) {
   for (var i = 0, len = arr.length; i < len; i++) {
     console.log(arr[i]);
   }
 }

아래와 동일
 function foo(arr) {
   var i, len;
   for (i = 0, len = arr.length; i < len; i++) {
     console.log(arr[i]);
   }
 }
함수 정의인 경우

 function foo() {
   bar(); //--> 'bar'
   function bar() {
     console.log('bar');
   }
 }

아래와 동일
 function foo() {
   function bar() {
     console.log('bar');
   }
   bar();
 }
함수 표현식인 경우 (변수 선언과 동일)

 function foo() {
   bar(); //--> ReferenceError
   var bar = function () {};
   bar(); // 호출되지 않음
 }

아래와 동일
 function foo() {
   var bar;
   bar();
   bar = function () {};
   bar();
 }
변수 선언은 호이스팅으로 맨 앞으로,
변수 할당은 실행시점에 수행됨.

호이스팅으로 헷갈릴 수 있으니,
함수의 맨 앞 부분에 한 개의 var만 쓸 것을 권장.

 function foo() {
   var a = 1,
     b = 2,
     c = 3;
 }
그치만, 눈을 크게 떠야할 때가 있음.

 function foo() {
   var a = 1,
     b = 3,
     c = 4;
     d = 2;
 }




오류 없이 실행되며, 변수 d는 전역으로 선언됨.
린트 툴을 사용할 것을 권장.
디버깅이 어려운 경우도 있음.

 function foo() {
   var a = 1,
     b = getComplexValue(a), // 브레이크 포인트를 잡기 어려움
     c = b + 3;
 }


상황에 따라 var로 나누는 것도 나쁘지 않은 선택.

 function foo() {
   var a = 1;
   var b = getComplexValue(a);
   var c = b + 3;
 }
또한, 분기문 내에 함수 정의를 넣지 말 것.

 function foo(condition) {
   if (condition) {
     function bar() {
       console.log('first bar');
     }
   } else {
     function bar() {
       console.log('second bar');
     }
   }
   bar();
 }
 foo(true); //--> 'second bar'
 foo(false); //--> 'second bar'
함수 정의 호이스팅에 따라 아래와 같기 때문.

 function foo(condition) {
   function bar() {
     console.log('first bar');
   }
   function bar() {
     console.log('second bar');
   }
   if (condition) {
   } else {
   }
   bar();
 }

* ES5 strict 모드에서는 SyntaxError 발생
상황에 따라 동적으로 할당하고 싶다면.
 function foo(condition) {
   var bar;
   if (condition) {
     bar = function () {
        console.log('first bar');
     };
   } else {
     bar = function () {
        console.log('second bar');
     };
   }
   bar();
 }
 foo(true); //--> 'first bar'
 foo(false); //--> 'second bar'
개인적으로는, var는 맨 위에,
함수는 읽어내려가기 편하게 작성하는 것을 선호함.

function foo() {
  var a, b, c;

    doAll();

    function doAll() {
      doA();
      doB();
    }
    function doA() {}
    function doB() {}
}
하지만 리턴문 뒤의 함수 정의는,
문맥 상 그다지 좋은 것 같지 않음.

 function foo() {
   return bar();

     function bar() {
       return ‘baz’;
     }
 }

 * 함수 정의 호이스팅으로 문제 없이 실행됨.
끝.
부록. 좀 더 자세히 알아보면.

 function test(a, b) {
   console.log(a, b, c, d);
   var c = 10;
   function d() {}
 }
 test(10);


위와 같은 함수가 전역 범위에서 실행될 때.
다음과 같은 식으로 실행됨.

1.    foo()가 호출되면,
2.    foo()의 실행 컨텍스트가 생성되고,
3.    실행 컨텍스트는 함수를 실행하기 전 활성화 객체를 생성한다.
4.    활성화 객체에는 파라미터 정보, 변수 선언, 함수 정의가 포함된다.
5.    함수가 실행되고 변수를 만났을 때엔,
     유효범위 체인에 따라 활성화 객체에서 먼저 찾고, 상위 탐색을 진행한다.
이 때, 활성화 객체에는 다음 값들이 할당된다.

 - 파라미터로 전달된 값
   파라미터의 이름과 값을 할당
   없는 경우 undefined로 할당

 - 모든 함수 선언
   함수의 이름과 정의를 할당
   이미 정의되어 있는 경우 덮어씀

 - 모든 변수 선언
   변수의 이름과 undefined를 할당
   이미 정의되어 있는 경우 할당하지 않음
즉, 아래 코드가 실행되기 전,
 function test(a, b) {
   console.log(a, b, c, d);
   var c = 10;
   function d() {}
 }
 test(10);

활성화 객체(Activation Object)는 다음과 같이 생성된다.
 AO = {
    a: 10,
    b: undefined,
    c: undefined,
    d: <reference to function ‘d’>
 };
실행 시점에서 변수를 만났을 때엔,
활성화 객체의 값을 찾거나 설정한다.

 function test(a, b) {
  console.log(a, b, c, d);
  // 각각 AO[‘a’], AO[‘b’], AO[‘c’], AO[‘d’]의 값
  // 10, undefined, undefined, function
  var c = 10; // AO[‘c’] = 10; 과 동일하다.
  function d() {}
 }
 test(10);
함수 표현식은 활성화 객체에 할당되지 않는다.

 function test() {
   var foo = function bar() {};
   // foo는 AO에 존재하지만, bar는 존재하지 않는다.
   // bar의 접근을 시도하면 ReferenceError가 발생한다.
   (function baz() {});
   // 마찬가지로 baz도 AO에 할당되지 않는다.
 }
호이스팅은 활성화 객체의 할당 방식에 따른 현상.

 function foo() {
   console.log(bar); //--> function ‘bar’
   // AO[‘bar’] = <reference to function ‘bar’>
   var bar = 10;
   function bar() {};
   var bar = 20;
 }
정말 끝.

Contenu connexe

Tendances

Secure coding-c-dcl-1
Secure coding-c-dcl-1Secure coding-c-dcl-1
Secure coding-c-dcl-1Seungyong Lee
 
호이스팅, 클로저, IIFE
호이스팅, 클로저, IIFE호이스팅, 클로저, IIFE
호이스팅, 클로저, IIFEChangHyeon Bae
 
Functional programming
Functional programmingFunctional programming
Functional programmingssuserdcfefa
 
ES6: RegExp.prototype.unicode 이해하기
ES6: RegExp.prototype.unicode 이해하기ES6: RegExp.prototype.unicode 이해하기
ES6: RegExp.prototype.unicode 이해하기Ohgyun Ahn
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)NAVER D2
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
 
Macro & compilation
Macro & compilationMacro & compilation
Macro & compilationIkhoon Eom
 
Haskell study 13
Haskell study 13Haskell study 13
Haskell study 13Nam Hyeonuk
 
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10Nam Hyeonuk
 
Python3 10장 문자열이야기
Python3 10장 문자열이야기Python3 10장 문자열이야기
Python3 10장 문자열이야기Jihoon Kong
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino영욱 김
 

Tendances (20)

Secure coding-c-dcl-1
Secure coding-c-dcl-1Secure coding-c-dcl-1
Secure coding-c-dcl-1
 
호이스팅, 클로저, IIFE
호이스팅, 클로저, IIFE호이스팅, 클로저, IIFE
호이스팅, 클로저, IIFE
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
6 function
6 function6 function
6 function
 
ES6: RegExp.prototype.unicode 이해하기
ES6: RegExp.prototype.unicode 이해하기ES6: RegExp.prototype.unicode 이해하기
ES6: RegExp.prototype.unicode 이해하기
 
[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)[devil's camp] - 알고리즘 대회와 STL (박인서)
[devil's camp] - 알고리즘 대회와 STL (박인서)
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
ES6 for Node.js Study 2주차
ES6 for Node.js Study 2주차ES6 for Node.js Study 2주차
ES6 for Node.js Study 2주차
 
Macro & compilation
Macro & compilationMacro & compilation
Macro & compilation
 
ES6 for Node.js Study
ES6 for Node.js StudyES6 for Node.js Study
ES6 for Node.js Study
 
Haskell study 13
Haskell study 13Haskell study 13
Haskell study 13
 
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
 
Haskell study 6
Haskell study 6Haskell study 6
Haskell study 6
 
Haskell study 5
Haskell study 5Haskell study 5
Haskell study 5
 
Python3 10장 문자열이야기
Python3 10장 문자열이야기Python3 10장 문자열이야기
Python3 10장 문자열이야기
 
iOS 메모리관리
iOS 메모리관리iOS 메모리관리
iOS 메모리관리
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
C Language For Arduino
C Language For ArduinoC Language For Arduino
C Language For Arduino
 

Similaire à Javascript hoisting

EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overviewyongwoo Jeon
 
Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Ji Hun Kim
 
Java jungsuk3 ch14_lambda_stream
Java jungsuk3 ch14_lambda_streamJava jungsuk3 ch14_lambda_stream
Java jungsuk3 ch14_lambda_stream성 남궁
 
[Swift] Functions
[Swift] Functions[Swift] Functions
[Swift] FunctionsBill Kim
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229Yong Joon Moon
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍Young-Beom Rhee
 

Similaire à Javascript hoisting (8)

EcmaScript6(2015) Overview
EcmaScript6(2015) OverviewEcmaScript6(2015) Overview
EcmaScript6(2015) Overview
 
Learning Node Book, Chapter 5
Learning Node Book, Chapter 5Learning Node Book, Chapter 5
Learning Node Book, Chapter 5
 
Javascript
JavascriptJavascript
Javascript
 
Java jungsuk3 ch14_lambda_stream
Java jungsuk3 ch14_lambda_streamJava jungsuk3 ch14_lambda_stream
Java jungsuk3 ch14_lambda_stream
 
[Swift] Functions
[Swift] Functions[Swift] Functions
[Swift] Functions
 
파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229파이썬+함수이해하기 20160229
파이썬+함수이해하기 20160229
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
 
javascript01
javascript01javascript01
javascript01
 

Plus de Ohgyun Ahn

호갱노노 이렇게 만듭니다
호갱노노 이렇게 만듭니다호갱노노 이렇게 만듭니다
호갱노노 이렇게 만듭니다Ohgyun Ahn
 
카카오스토리 웹팀의 코드리뷰 경험
카카오스토리 웹팀의 코드리뷰 경험카카오스토리 웹팀의 코드리뷰 경험
카카오스토리 웹팀의 코드리뷰 경험Ohgyun Ahn
 
Node.js 시작하기
Node.js 시작하기Node.js 시작하기
Node.js 시작하기Ohgyun Ahn
 
JavaScript Memory Profiling
JavaScript Memory ProfilingJavaScript Memory Profiling
JavaScript Memory ProfilingOhgyun Ahn
 
JavaScript Minification
JavaScript MinificationJavaScript Minification
JavaScript MinificationOhgyun Ahn
 
JavaSript Template Engine
JavaSript Template EngineJavaSript Template Engine
JavaSript Template EngineOhgyun Ahn
 
Github Usage Scenarios
Github Usage ScenariosGithub Usage Scenarios
Github Usage ScenariosOhgyun Ahn
 
diff output formats
diff output formatsdiff output formats
diff output formatsOhgyun Ahn
 
패키지 매니저의 요건
패키지 매니저의 요건패키지 매니저의 요건
패키지 매니저의 요건Ohgyun Ahn
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide SummaryOhgyun Ahn
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Ohgyun Ahn
 
UX 심포지엄 20120 키노트 정리
UX 심포지엄 20120 키노트 정리UX 심포지엄 20120 키노트 정리
UX 심포지엄 20120 키노트 정리Ohgyun Ahn
 
크롬익스텐션 맛보기
크롬익스텐션 맛보기크롬익스텐션 맛보기
크롬익스텐션 맛보기Ohgyun Ahn
 
재미있는 생산성 향상 도구
재미있는 생산성 향상 도구재미있는 생산성 향상 도구
재미있는 생산성 향상 도구Ohgyun Ahn
 
Raphael.js로 SVG 차트 만들기
Raphael.js로 SVG 차트 만들기Raphael.js로 SVG 차트 만들기
Raphael.js로 SVG 차트 만들기Ohgyun Ahn
 
깃헙으로 코드리뷰 하기
깃헙으로 코드리뷰 하기깃헙으로 코드리뷰 하기
깃헙으로 코드리뷰 하기Ohgyun Ahn
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relationOhgyun Ahn
 

Plus de Ohgyun Ahn (17)

호갱노노 이렇게 만듭니다
호갱노노 이렇게 만듭니다호갱노노 이렇게 만듭니다
호갱노노 이렇게 만듭니다
 
카카오스토리 웹팀의 코드리뷰 경험
카카오스토리 웹팀의 코드리뷰 경험카카오스토리 웹팀의 코드리뷰 경험
카카오스토리 웹팀의 코드리뷰 경험
 
Node.js 시작하기
Node.js 시작하기Node.js 시작하기
Node.js 시작하기
 
JavaScript Memory Profiling
JavaScript Memory ProfilingJavaScript Memory Profiling
JavaScript Memory Profiling
 
JavaScript Minification
JavaScript MinificationJavaScript Minification
JavaScript Minification
 
JavaSript Template Engine
JavaSript Template EngineJavaSript Template Engine
JavaSript Template Engine
 
Github Usage Scenarios
Github Usage ScenariosGithub Usage Scenarios
Github Usage Scenarios
 
diff output formats
diff output formatsdiff output formats
diff output formats
 
패키지 매니저의 요건
패키지 매니저의 요건패키지 매니저의 요건
패키지 매니저의 요건
 
BASH Guide Summary
BASH Guide SummaryBASH Guide Summary
BASH Guide Summary
 
Python Usage (5-minute-summary)
Python Usage (5-minute-summary)Python Usage (5-minute-summary)
Python Usage (5-minute-summary)
 
UX 심포지엄 20120 키노트 정리
UX 심포지엄 20120 키노트 정리UX 심포지엄 20120 키노트 정리
UX 심포지엄 20120 키노트 정리
 
크롬익스텐션 맛보기
크롬익스텐션 맛보기크롬익스텐션 맛보기
크롬익스텐션 맛보기
 
재미있는 생산성 향상 도구
재미있는 생산성 향상 도구재미있는 생산성 향상 도구
재미있는 생산성 향상 도구
 
Raphael.js로 SVG 차트 만들기
Raphael.js로 SVG 차트 만들기Raphael.js로 SVG 차트 만들기
Raphael.js로 SVG 차트 만들기
 
깃헙으로 코드리뷰 하기
깃헙으로 코드리뷰 하기깃헙으로 코드리뷰 하기
깃헙으로 코드리뷰 하기
 
Recurrence relation
Recurrence relationRecurrence relation
Recurrence relation
 

Javascript hoisting

  • 2. 먼저, 지역 변수의 유효 범위는 블럭이 아닌 함수! function foo() { var bar = ‘bar’; console.log(bar); //--> ‘bar’ if (true) { var bar = ‘baz’; console.log(bar); //--> ‘baz’ } console.log(bar); //--> ‘baz’ }
  • 3. 즉시 실행 함수로 유효 범위를 제한할 수 있음. function foo() { var bar = ‘bar’; console.log(bar); //--> ‘bar’ if (true) { (function () { var bar = ‘baz’; console.log(bar); //--> ‘baz’ }()); } console.log(bar); //--> ‘bar’ }
  • 4. 호이스팅(Hoisting) 변수 선언과 함수 선언은 인터프리터에 의해 함수의 맨 앞으로 끌어올려진다(옮겨진다). * hoist = 끌어올리다
  • 5. 용어가 좀 헷갈릴 수 있다. 변수 선언(Variable Declaration) var foo = ‘bar’; 함수 선언(Function Declaration) function foo() {} 함수 표현식(Function Expression) var foo = function () {}; (function foo() {});
  • 6. 변수 선언인 경우 function foo() { console.log(bar); //--> undefined var bar = 'baz'; console.log(bar); //--> 'baz' } 아래와 동일 function foo() { var bar; // 변수 선언은 함수의 맨 앞으로 끌어올려진다.(hoisted) console.log(bar); bar = 'baz'; // 할당은 실행 시점에 수행 console.log(bar); }
  • 7. for 문의 경우도 동일 function foo(arr) { for (var i = 0, len = arr.length; i < len; i++) { console.log(arr[i]); } } 아래와 동일 function foo(arr) { var i, len; for (i = 0, len = arr.length; i < len; i++) { console.log(arr[i]); } }
  • 8. 함수 정의인 경우 function foo() { bar(); //--> 'bar' function bar() { console.log('bar'); } } 아래와 동일 function foo() { function bar() { console.log('bar'); } bar(); }
  • 9. 함수 표현식인 경우 (변수 선언과 동일) function foo() { bar(); //--> ReferenceError var bar = function () {}; bar(); // 호출되지 않음 } 아래와 동일 function foo() { var bar; bar(); bar = function () {}; bar(); }
  • 10. 변수 선언은 호이스팅으로 맨 앞으로, 변수 할당은 실행시점에 수행됨. 호이스팅으로 헷갈릴 수 있으니, 함수의 맨 앞 부분에 한 개의 var만 쓸 것을 권장. function foo() { var a = 1, b = 2, c = 3; }
  • 11. 그치만, 눈을 크게 떠야할 때가 있음. function foo() { var a = 1, b = 3, c = 4; d = 2; } 오류 없이 실행되며, 변수 d는 전역으로 선언됨. 린트 툴을 사용할 것을 권장.
  • 12. 디버깅이 어려운 경우도 있음. function foo() { var a = 1, b = getComplexValue(a), // 브레이크 포인트를 잡기 어려움 c = b + 3; } 상황에 따라 var로 나누는 것도 나쁘지 않은 선택. function foo() { var a = 1; var b = getComplexValue(a); var c = b + 3; }
  • 13. 또한, 분기문 내에 함수 정의를 넣지 말 것. function foo(condition) { if (condition) { function bar() { console.log('first bar'); } } else { function bar() { console.log('second bar'); } } bar(); } foo(true); //--> 'second bar' foo(false); //--> 'second bar'
  • 14. 함수 정의 호이스팅에 따라 아래와 같기 때문. function foo(condition) { function bar() { console.log('first bar'); } function bar() { console.log('second bar'); } if (condition) { } else { } bar(); } * ES5 strict 모드에서는 SyntaxError 발생
  • 15. 상황에 따라 동적으로 할당하고 싶다면. function foo(condition) { var bar; if (condition) { bar = function () { console.log('first bar'); }; } else { bar = function () { console.log('second bar'); }; } bar(); } foo(true); //--> 'first bar' foo(false); //--> 'second bar'
  • 16. 개인적으로는, var는 맨 위에, 함수는 읽어내려가기 편하게 작성하는 것을 선호함. function foo() { var a, b, c; doAll(); function doAll() { doA(); doB(); } function doA() {} function doB() {} }
  • 17. 하지만 리턴문 뒤의 함수 정의는, 문맥 상 그다지 좋은 것 같지 않음. function foo() { return bar(); function bar() { return ‘baz’; } } * 함수 정의 호이스팅으로 문제 없이 실행됨.
  • 18. 끝.
  • 19. 부록. 좀 더 자세히 알아보면. function test(a, b) { console.log(a, b, c, d); var c = 10; function d() {} } test(10); 위와 같은 함수가 전역 범위에서 실행될 때.
  • 20. 다음과 같은 식으로 실행됨. 1. foo()가 호출되면, 2. foo()의 실행 컨텍스트가 생성되고, 3. 실행 컨텍스트는 함수를 실행하기 전 활성화 객체를 생성한다. 4. 활성화 객체에는 파라미터 정보, 변수 선언, 함수 정의가 포함된다. 5. 함수가 실행되고 변수를 만났을 때엔, 유효범위 체인에 따라 활성화 객체에서 먼저 찾고, 상위 탐색을 진행한다.
  • 21. 이 때, 활성화 객체에는 다음 값들이 할당된다. - 파라미터로 전달된 값 파라미터의 이름과 값을 할당 없는 경우 undefined로 할당 - 모든 함수 선언 함수의 이름과 정의를 할당 이미 정의되어 있는 경우 덮어씀 - 모든 변수 선언 변수의 이름과 undefined를 할당 이미 정의되어 있는 경우 할당하지 않음
  • 22. 즉, 아래 코드가 실행되기 전, function test(a, b) { console.log(a, b, c, d); var c = 10; function d() {} } test(10); 활성화 객체(Activation Object)는 다음과 같이 생성된다. AO = { a: 10, b: undefined, c: undefined, d: <reference to function ‘d’> };
  • 23. 실행 시점에서 변수를 만났을 때엔, 활성화 객체의 값을 찾거나 설정한다. function test(a, b) { console.log(a, b, c, d); // 각각 AO[‘a’], AO[‘b’], AO[‘c’], AO[‘d’]의 값 // 10, undefined, undefined, function var c = 10; // AO[‘c’] = 10; 과 동일하다. function d() {} } test(10);
  • 24. 함수 표현식은 활성화 객체에 할당되지 않는다. function test() { var foo = function bar() {}; // foo는 AO에 존재하지만, bar는 존재하지 않는다. // bar의 접근을 시도하면 ReferenceError가 발생한다. (function baz() {}); // 마찬가지로 baz도 AO에 할당되지 않는다. }
  • 25. 호이스팅은 활성화 객체의 할당 방식에 따른 현상. function foo() { console.log(bar); //--> function ‘bar’ // AO[‘bar’] = <reference to function ‘bar’> var bar = 10; function bar() {}; var bar = 20; }