Hoisting, Scope Chaining

๊น€ํ˜„ํ•™ยท2023๋…„ 9์›” 24์ผ
1

Temp

๋ชฉ๋ก ๋ณด๊ธฐ
6/8

๐Ÿ”ฅ๐Ÿ•บ๐Ÿผ Hoisting ์‹œ๊ฐํ™” ์ž๋ฃŒ

๋ณ€์ˆ˜ ํ• ๋‹น

์ง€์ •๋œ ์ดˆ๊ธฐ ๊ฐ’ ์—†์ดย varย ํ˜น์€ย letย ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š”ย undefined ๊ฐ’์„ ๊ฐ–์Šต๋‹ˆ๋‹ค.

์„ ์–ธ๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์— ์ ‘๊ทผ์„ ์‹œ๋„ํ•˜๋Š” ๊ฒฝ์šฐย ReferenceErrorย ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.


๋ถ€์—ฐ ์„ค๋ช…

#1 Creation Phase

์ฝ”๋“œ์˜ Lexical scope๊ฐ€ Global scope์ธ ๋ช…๋ น์— ํ•œํ•˜์—ฌ ์œ„์—์„œ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ํ›‘๋Š”๋‹ค.

  1. ํ•จ์ˆ˜ ์ƒ์„ฑ
    function ํ‚ค์›Œ๋“œ๋กœ ์ •์˜๋œ ํ•จ์ˆ˜์˜ ์ด๋ฆ„์„ ๋ณ€์ˆ˜๋ช…์œผ๋กœ ํ•˜๊ณ ,
    ๊ตฌํ˜„๋ถ€์— ๋Œ€ํ•œ reference๋ฅผ ์ €์žฅํ•œ๋‹ค.

๐Ÿ’ก ๋‹จ, const foo = function x(){ โ€ฆ } ๋Š” const๋กœ ์ •์˜๋œ ๋ณ€์ˆ˜ foo์— ๋Œ€ํ•œ ์ •์˜์ด๋ฏ€๋กœ,function ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.

  1. ๋ณ€์ˆ˜ ์ƒ์„ฑ
    var, let, const ๊ฐ ํ‚ค์›Œ๋“œ ๋ณ„ ๋ณ€์ˆ˜์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋”ฐ๋ผ ์„ ์–ธ ๋˜๋Š” ์ดˆ๊ธฐํ™”๊นŒ์ง€ ์ง„ํ–‰ํ•œ๋‹ค.

#2 Execution Phase

Global scope์˜ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์œ„์—์„œ๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•œ ์ค„์”ฉ ์‹คํ–‰ํ•œ๋‹ค.

  1. ํ•จ์ˆ˜์˜ ์‹คํ–‰ ๋ช…๋ น์€ call stack์— ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ์Œ“์•„ ์‹คํ–‰ํ•œ๋‹ค.
  2. ๋ณ€์ˆ˜(๋˜๋Š” ํ•จ์ˆ˜)์˜ ํ• ๋‹น ๋ช…๋ น์€ ๋ณ€์ˆ˜๋ช…์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.

๐Ÿ’ก Block scope์™€ ๊ด€๊ณ„์—†์ด ๋ชจ๋“  ์„ ์–ธ์€ ์†Œ์†๋œ ํ•ด๋‹น scope์— ๋Œ€ํ•œ Creation Phase์—์„œ declaration ์„ ์–ธ์„ ์ง„ํ–‰ํ•œ๋‹ค.

(๋‹จ, var ๋ณ€์ˆ˜๋‚˜ function ํ‚ค์›Œ๋“œ๋กœ ์ •์˜๋œ ํ•จ์ˆ˜๋ผ๋ฉด
์‹คํ–‰ ์ดํ›„ ๊ฒฐ๊ณผ๋Š” Block Scope ์•ˆ์— ์†ํ•˜์ง€ ์•Š๊ณ , ์ตœ์ƒ์œ„ Scope์— ์†ํ•˜๊ฒŒ ๋œ๋‹ค.)


์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ ์˜ˆ์ƒํ•˜๊ธฐ

Prerequisite

Understanding Javascript Engine Phases, Global, Lexical, Block scope From JS: ๋ณ€์ˆ˜์„ ์–ธ๊ณผ Scope

  1. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  Phase์—์„œ ์ฝ”๋“œ๋Š” ์œ„์—์„œ๋ถ€ํ„ฐ ํ•œ ์ค„์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ์ฝ๋Š”๋‹ค.
  2. Call Stack์ด ํ™œ์„ฑํ™”๋˜๋ฉฐ, ํ•จ์ˆ˜๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์€
    Creation Phase์˜ ์ข…๋ฃŒ ํ›„โ€”Execution Phase์ด๋‹ค.

์˜ˆ์ œ

console.log(a); // undefined
var a;

๐Ÿ’ก ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐํ™” ์‹œ์ ์ด ๋‹ค๋ฅด๋‹ค.

var a;
console.log(a); // *f* a() {}
function a() {}
console.log(a); // *f* a() {}
var a = 1;
console.log(a); // 1
function a() {}
console.log(a); // 1
function a() {}
console.log(a); // *f* a() {}
var a = 1;
console.log(a); // 1

๐Ÿ’ก ๋™์ผํ•œ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค๋ฉด, ์ดํ›„ ๋‚ด์šฉ์ด ์ด์ „ ๋‚ด์šฉ์„ Overrideํ•œ๋‹ค.

function a() { 1; }
console.log(a); // ฦ’ a() {}
function a() {}
console.log(a); // ฦ’ a() {}
var a = 1;
console.log(a); // 1

๐Ÿ’ก Block scope๋Š” Creation Phase๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ , Execution Phase์—๋งŒ ์‹คํ–‰๋œ๋‹ค.

{  function a() { 1; } }
console.log(a); // ฦ’ a() { 1; }
function a() {}
console.log(a); // ฦ’ a() { 1; }
var a = 1;
console.log(a); // 1
scope chaining function scope === local scope

์šฐ๋ฆฌ๊ฐ€ ์‹คํ–‰ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋Š” ์ฝœ ์Šคํƒ์— ์˜ฌ๋ผ์˜ค๋Š” ํ•จ์ˆ˜๋ž‘ ๋˜‘๊ฐ™๋‹ค.

์ฆ‰, ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋Š” ๋˜ ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

Creation Phase๋ถ€ํ„ฐ Execution๊นŒ์ง€ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ

๊ทธ ์•ˆ์— ์—†์œผ๋ฉด ์–‘ํŒŒ ๊ป์งˆ ๊นŒ๋“ฏ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์Šค์ฝ”ํ”„์—์„œ ์ฐพ์•„์˜ด

๊ทธ๊ฒŒ ๋ฐ”๋กœ scope chaining

๐Ÿ’ก scope chaining
๊ฐ€์žฅ ๊ฐ€๊นŒ์šด scope์— ์กด์žฌํ•˜๋Š” ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

function a() {
  console.log(a); // ฦ’ a() { console.log(a); } 
}
a();
function a() {
  console.log(a); // undefined
  var a = 1;
}
a();

๐Ÿ’ก scope chaining ์‘์šฉ

function a() {
  console.log(eval); // undefined
  {
    function eval() {}
  }
  console.log(eval); // ฦ’ eval() {} 
}
console.log(eval); // ฦ’ eval()
a();
console.log(eval); // ฦ’ eval()
console.log(a); // undefined
{
	function a() {}
}

๐Ÿ’ก scope chaining: let๊ณผ var์˜ ์ฐจ์ด

function a() {
  console.log(b); // undefined
}
let b;
a();
function a() {
  console.log(b); // ReferenceError
}
a();
let b;
function a() {
  console.log(b); // undefined
}
a();
var b;

var์™€ function์€ Creation Phase์— ์ •์˜๋  ๋•Œ,
์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„์— ์ •์˜๋œ๋‹ค.

๋”ฐ๋ผ์„œ, function์˜ ๊ตฌํ˜„๋ถ€๋ผ๋ฉด function scope == local scope์— ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.

Event Loop์— ๋Œ€ํ•ด์„  Event Loop ์ฐธ๊ณ 

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด