[JS] πŸ“ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ ⭐

Mec.D's BlogΒ·2022λ…„ 5μ›” 31일
0

JS ES6+ 비곡식 λ…ΈνŠΈ

λͺ©λ‘ 보기
7/17
post-thumbnail

πŸ’‘ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ (Execution Context)

ν•¨μˆ˜ μ‹€ν–‰ μ‹œ ν•„μš”ν•œ 쑰건 / ν™˜κ²½ 정보λ₯Ό 담은 κ°μ²΄μž…λ‹ˆλ‹€.


νŠΉμ§•

  1. JS 엔진이 μ»΄νŒŒμΌλ ˆμ΄μ…˜μ„ μˆ˜ν–‰ν•˜λŠ” λ‹¨μœ„μž…λ‹ˆλ‹€.

κ΅¬μ„±μš”μ†Œ

VariableEnvironmentΒ  LexicalEnvironmentΒ  ThisBinding

μš”μ†Œμ„€λͺ…
VariableEnvironmentν˜„μž¬μ˜ μ‹€ν–‰ ν™˜κ²½κ³Ό κ΄€λ ¨λœ μ‹λ³„μžλ₯Ό μˆ˜μ§‘ν•΄ μŠ€λƒ…μƒ·μœΌλ‘œ λ§Œλ“­λ‹ˆλ‹€.
LexicalEnvironmentν˜„μž¬μ˜ μ‹€ν–‰ ν™˜κ²½κ³Ό κ΄€λ ¨λœ μ‹λ³„μžμ— λ‹΄κΈ΄ 데이터λ₯Ό μΆ”μ ν•©λ‹ˆλ‹€.
ThisBindingν•¨μˆ˜ 호좜 (Function.prototype.call) μ‹œ λ§€κ°œλ³€μˆ˜λ‘œ λ„˜κ²¨λ°›μ€ this의 μ°Έμ‘°κ°’μž…λ‹ˆλ‹€.

VariableEnvironment와 LexicalEnvironment 비ꡐ

LexicalEnvironmentVariableEnvironment
μš©λ„μ‹λ³„μžμ— λ‹΄κΈ΄ 데이터λ₯Ό μΆ”μ ν•˜λŠ” μš©λ„μ‹λ³„μž μˆ˜μ§‘ μš©λ„
κ°’ λ³€ν™”βœ… μ‹€μ‹œκ°„ λ³€ν™” 반영❌ Never changes
κ΅¬μ„±μš”μ†ŒenvironmentRecord
: μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμ˜ creation phase에
ν˜„μž¬ μ»¨ν…μŠ€νŠΈμ˜ μ‹λ³„μž 정보 μˆ˜μ§‘

Hoisting이 μΌμ–΄λ‚©λ‹ˆλ‹€.

outerEnvironmentReference
: μ™ΈλΆ€ ν™˜κ²½ μ°Έμ‘°
μŠ€μ½”ν”„ 체이닝이 μΌμ–΄λ‚©λ‹ˆλ‹€.
environmentRecord와
outerEnvironmentRecord의 μŠ€λƒ…μƒ·

λ™μž‘ 방식

πŸ…° Creation Phase (Compilation)

γ€Žλ³€μˆ˜/ν•¨μˆ˜ μ„ μ–Έ, λ§€κ°œλ³€μˆ˜, scope 정보 μˆ˜μ§‘γ€

  1. VariableEnvironment 생성
  2. this λ§€κ°œλ³€μˆ˜ 전달 (this 바인딩)
  3. LexicalEnvironment 생성 (VariableEnvironment의 사본)
  4. ν•΄λ‹Ή μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ 객체λ₯Ό 콜 μŠ€νƒμ— push

πŸ…± Execution Phase

γ€Žκ°’ ν• λ‹Ή 및 ꡬ문 싀행』

  1. μ½”λ“œ μ‹€ν–‰ 및 κ²°κ³Ό μ €μž₯
  2. (더 μ‹€ν–‰ν•  μ½”λ“œκ°€ μ—†μœΌλ©΄) μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ pop ν›„ return κ°’ λ°˜ν™˜

μ’…λ₯˜

πŸ‘‰ Functional EC
ν•¨μˆ˜ λ²”μœ„μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.

🌏 Global EC
μ „μ—­ λ²”μœ„μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.
ν”„λ‘œκ·Έλž¨κ³Ό 생λͺ…μ£ΌκΈ°λ₯Ό κ°™μ΄ν•©λ‹ˆλ‹€.

🧩 Moudule EC
λͺ¨λ“ˆ λ²”μœ„μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.
import 될 λ•Œ μƒμ„±λ˜κ³  λͺ¨λ“ˆ μ½”λ“œκ°€ λλ‚˜λ©΄ μ’…λ£Œλ©λ‹ˆλ‹€.

🚫 Eval EC (Deperecated)
eval() 을 μ‹€ν–‰ν–ˆμ„ λ•Œ μƒμ„±λ˜λŠ” μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμž…λ‹ˆλ‹€.
λ³΄μ•ˆμƒ 취약점을 κ°–κ³  μžˆμ–΄ 거의 μ‚¬μš©λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

profile
기술둜 더 λ‚˜μ€ 미래λ₯Ό λ””μžμΈν•˜λŠ” 개발자 MEC:D μž…λ‹ˆλ‹€

0개의 λŒ“κΈ€