๐ก Execution Context๋?
์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๊ฐ ์คํ๋๋ย ํ๊ฒฝ์ ์ถ์ํํ ๊ฐ์ฒด์ด๋ค. ์ฝ๋๊ฐ ์คํ๋ ๋ ํ์ํ ํ๊ฒฝ ์ ๋ณด๋ค์ ๋ชจ์ ๋์ ๊ฐ์ฒด๋ผ๊ณ ํ ์ ์๋ค.
1. ์คํ ํ๊ฒฝ์ ๋ง๋ ๋ค
2. ๊ทธ ์์์ ์ฝ๋๋ฅผ ์คํํ๋ค
์์ง์ ์ฝ๋๋ฅผ ๊ทธ๋ฅ ์คํํ์ง ์๊ณ โ์คํ ํ๊ฒฝโ์ ๋จผ์ ๋ง๋ ๋ค์ ๊ทธ ์์์ ์คํํ๋ค.
์ด ํ๊ฒฝ์ด ๋ฐ๋ก Execution Context๋ค.
๐ฆ Execution Context ์ข ๋ฅ
- Global Execution Context (์ ์ญ ์คํ ์ปจํ
์คํธ)
- ์ฝ๋๊ฐ ์ฒ์ ์คํ๋ ๋ ์์ฑํ๋ค.
- Function Execution Context (ํจ์ ์คํ ์ปจํ
์คํธ)
- ํจ์ ํธ์ถ๋ ๋๋ง๋ค ์์ฑํ๋ค.
- Eval Execution Context (eval ์คํ ์ปจํ
์คํธ)
- eval ํจ์ ๋ด์์ ์คํ๋๋ค.
์คํ ํ๋ฆ
1. Global Execution Context ์์ฑ
2. ๋ณ์/ํจ์ ๋ฑ๋ก
3. ํจ์ ํธ์ถ โ Function Execution Context ์์ฑ
4. ์คํ ํ ์ฌ๋ผ์ง
๐ง Execution Context ๋ด๋ถ ๊ตฌ์กฐ
Lexical Environment
- let / const
- ํจ์ ์ ์ธ
- ๋ธ๋ก ์ค์ฝํ ์ ๋ณด / ์์ ์ค์ฝํ ์ฐธ์กฐ
Variable Environment
- var ์ ์ฉ ์ ์ฅ์ (์ด๊ธฐ JS ํธํ์ฉ ๊ฐ๋ )
This Binding
- ํ์ฌ ์คํ ์ปจํ ์คํธ์์์ this ๊ฐ
๐ฅ Execution Context๋ 2๋จ๊ณ๋ก ์์ฑ๋๋ค
Execution Context๋ ์ฝ๋ ์คํ์ ์ํ ํ๊ฒฝ์ด๋ฉฐ, ์์ฑ ๋จ๊ณ์์ ๋ณ์/ํจ์ ์ ์ธ์ด ๋ฑ๋ก๋๊ณ , ์คํ ๋จ๊ณ์์ ์ฝ๋๊ฐ ์ํ๋๋ค.
Creation Phase (์์ฑ ๋จ๊ณ)
- ์์ง์ด ์์ง ์ฝ๋ ์คํ ์ ํ ๋จ๊ณ์์ ๋ณ์ ์ ์ธ, ํจ์ ์ ์ธ, this ๊ฒฐ์ ์ ๋ฉ๋ชจ๋ฆฌ์ ๋ฑ๋กํ๋ค.
- ์ด ๋จ๊ณ ๋๋ฌธ์ Hoisting์ด ์๊ธด๋ค.
Execution Phase (์คํ ๋จ๊ณ)
- ์ฝ๋๋ฅผ ํ ์ค์ฉ ์คํํ๊ณ ๋ณ์์ ๊ฐ์ ํ ๋นํ๊ณ ํจ์๋ฅผ ํธ์ถํ๋ค.
๐ก Hoisting์ด๋?
โ์ ์ธ์ด ์๋ก ๋์ด์ฌ๋ ค์ง ๊ฒ์ฒ๋ผ ๋ณด์ด๋ ํ์โ
ํ์ง๋ง ์ค์ ๋ก๋ Creation Phase์์ ์ ์ธ์ด ๋ฏธ๋ฆฌ ๋ฑ๋ก๋์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ ๊ฒ ๋ณด์ด๋ ๊ฒ์ด๋ค.
๐ var์ ๊ฒฝ์ฐ: undefined ์ถ๋ ฅ
console.log(a);
var a = 10;
1. Global Execution Context ์์ฑ
Creation Phase๋ฅผ ์คํํ๊ณ ์์ง์ var a๋ฅผ Variable Environment์ ๋ฑ๋กํ๊ณ ์ด๊ธฐ๊ฐ์ undefined๋ก ์ธํ
ํ๋ค.
// ๋ฉ๋ชจ๋ฆฌ ์ํ
a: undefined
2. Execution Phase ์์ : ์ฝ๋๊ฐ ํ ์ค์ฉ ์คํ๋๋ค.
// ์ฒซ๋ฒ์งธ ์ค
console.log(a);
์์ง์ Execution Context์ Variable Environment์์ ๊ฐ์ด undefined์ธ a๋ฅผ ์ฐพ์์ undefined๋ฅผ ์ถ๋ ฅํ๋ค.
// ๋๋ฒ์งธ ์ค
var a = 10;
์ด๋ฏธ a๋ ์กด์ฌํ์ผ๋ฏ๋ก ์์ง์ a ์ 10 ์ด๋ผ๋ ๊ฐ์ ํ ๋นํ๋ค.
๐ย let & const์ ๊ฒฝ์ฐ: ReferenceError ๋ฐ์
1. Global Execution Context ์์ฑ
Creation Phase๋ฅผ ์คํํ๊ณ ์์ง์ let a๋ฅผ Lexical Environment์ ๋ฑ๋กํ์ง๋ง ์ด๊ธฐํํ์ง ์๋๋ค. ์ด ์ํ๋ฅผ TDZ (Temporal Dead Zone) ๋ผ๊ณ ํ๋ค.
// ๋ฉ๋ชจ๋ฆฌ ์ํ
a: <uninitialized>
2. Execution Phase ์์ : ์ฝ๋๊ฐ ํ ์ค์ฉ ์คํ๋๋ค.
// ์ฒซ๋ฒ์งธ ์ค
console.log(a);
์์ง์ ์กด์ฌ๋ ํ์ง๋ง ์ด๊ธฐํ ๋์ง ์์ a๋ก ์ธํด์ ์๋ฌ๋ฅผ ๋ฐ์์ํต๋๋ค.
| ๊ตฌ๋ถ | var | let |
|---|---|---|
| ๋ฑ๋ก ์์น | Variable Environment | Lexical Environment |
| Creation Phase ์ด๊ธฐํ | undefined | ์ด๊ธฐํ ์ ๋จ |
| ์ด๊ธฐํ ์ ์ ๊ทผ | undefined ์ถ๋ ฅ | ReferenceError |
๐ ํจ์์ ๊ฒฝ์ฐ: ํธ์ถ ๊ฐ๋ฅ
foo();
function foo() {
console.log("hello");
}
1. Creation Phase ์์ :
foo: function object
์ด๋ฏธ ํจ์๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ๋ฑ๋ก๋๊ธฐ์ ํธ์ถ์ด ๊ฐ๋ฅํ๋ค.
๐ ํจ์ ํํ์์ ๊ฒฝ์ฐ: ์๋ฌ ๋ฐ์
foo();
var foo = function () {
console.log("hello");
};
1. Creation Phase๋ฅผ ์คํํ๋ค :
foo: undefined
2. Execution Phase ์์ : ์ฝ๋๊ฐ ํ ์ค์ฉ ์คํ๋๋ค.
// ์ฒซ๋ฒ์งธ ์ค
foo(); โ undefined is not a function โ
์๋๋ฉด ํจ์ ๊ฐ์ฒด๋ ์์ง ํ ๋น ์ ๋๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋ฐ์๋๋ค.
๐ย ์กฐ๊ธ ๋ ๋ณต์กํ ์์
var a = 1;
function test() {
console.log(a);
var a = 2;
}
test();
1. ํ๋ก๊ทธ๋จ ์์
JS๋ ์ฝ๋๋ฅผ ์คํํ๊ธฐ ์ ์ ๋จผ์ Global Execution Context๋ฅผ ๋ง๋ ๋ค.
a: undefined
test: function object
1. Global Creation Phase
var a๋ฑ๋ก โa: undefinedfunction test๋ฑ๋ก โ ํจ์ ๊ฐ์ฒด๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ
2. Global Execution Phase (์ฝ๋๋ฅผ ํ ์ค์ฉ ์คํํ๋ค)
-
var a = 1: ์ด๋ฏธ a๋ ์กด์ฌํ๋ฏ๋ก ๊ฐ๋ง ๋ณ๊ฒฝํ๋คa = 1 -
test()ํธ์ถ
2. test() ํธ์ถ
1. Function Execution Context ์์ฑ
test()๊ฐ ํธ์ถ๋๋ฉด ์๋ก์ด Function Execution Context๊ฐ ๋ง๋ค์ด์ง๋ค.
2. test ํจ์์ Creation Phase
a: undefined
- ์์ง์ด ๋จผ์ ๋ด๋ถ ์ ์ธ์ ์ฒ๋ฆฌํ๋ค.
var a๋ฑ๋ก โa: undefined- ์ฌ๊ธฐ์ ํต์ฌ์ ์ด
a๋ global a์ ๋ค๋ฅธ ๋ณ์๋ผ๋ ๊ฒ์ด๋ค.
3. test ํจ์ Execution Phase (์ฝ๋๋ฅผ ํ ์ค์ฉ ์คํํ๋ค)
-
console.log(a):- ์์ง์
a๋ฅผ ์ฐพ๋๋ค - ํ์ ์์:
- test ํจ์ ์ค์ฝํ์์
a๋ฅผ ๋ฐ๊ฒฌํ๋ค (undefined) - ๊ทธ๋์ global๊น์ง ๊ฐ์ง ์๋๋ค
- test ํจ์ ์ค์ฝํ์์
- ์ถ๋ ฅ:
undefined
- ์์ง์
-
var a = 2:a = 2์ด๋ฏธ
a๋ ์กด์ฌํ๋ฏ๋ก ๊ฐ๋ง ํ ๋นํ๋ค
3. test() ์ข ๋ฃ
Function Execution Context๊ฐ Call Stack์์ ์ ๊ฑฐ๋๋ค.
๐งถ ์ ์ฒด ํ๋ฆ ์ ๋ฆฌ
GlobalExecutionContext ์์ฑ
โGlobalCreationPhase
-a:undefined
-test:function
โGlobalExecutionPhase
- a =1
-test() ํธ์ถ
โFunctionExecutionContext ์์ฑ
โCreationPhase
-a:undefined
โExecutionPhase
-console.log(a) โundefined
- a =2
โ
ํจ์ ์ข
๋ฃ โ ์ ๊ฑฐ
