Skip to content

Commit

Permalink
docs: day8(24.1 ~ 24.3) (#1000)
Browse files Browse the repository at this point in the history
  • Loading branch information
byunjiin authored Dec 12, 2023
1 parent f4eca37 commit 70a2dbe
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions docs/24_ν΄λ‘œμ €/변지인.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
## ν΄λ‘œμ €

**였늘 읽은 λ²”μœ„** : 24.1 ~ 24.3

### 0. ν΄λ‘œμ €

---

#### _ν΄λ‘œμ €(closure)_

μžλ°”μŠ€ν¬λ¦½νŠΈ 고유의 κ°œλ…μ΄ μ•„λ‹Œ, ν•¨μˆ˜λ₯Ό 일급 객체둜 μ·¨κΈ‰ν•˜λŠ” `ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° μ–Έμ–΄`(ν•˜μŠ€μΌˆ, λ¦¬μŠ€ν”„, μ–Όλž­, 슀칼라 λ“±)μ—μ„œ μ‚¬μš©λ˜λŠ” μ€‘μš”ν•œ `νŠΉμ§•`이닀.

```
ν΄λ‘œμ € = ν•¨μˆ˜ + λ ‰μ‹œμ»¬ ν•¨μˆ˜
```

### 1. λ ‰μ‹œμ»¬ μŠ€μ½”ν”„

---

#### _λ ‰μ‹œμ»¬ μŠ€μ½”ν”„(정적 μŠ€μ½”ν”„)_

μžλ°”μŠ€ν¬λ¦½νŠΈ 엔진은 ν•¨μˆ˜λ₯Ό μ–΄λ””μ„œ ν˜ΈμΆœν–ˆλŠ”μ§€κ°€ μ•„λ‹ˆλΌ `ν•¨μˆ˜λ₯Ό 어디에 μ •μ˜ν–ˆλŠ”μ§€μ— 따라 μƒμœ„ μŠ€μ½”ν”„λ₯Ό κ²°μ •`ν•˜λŠ”λ° 이걸 **λ ‰μ‹œμ»¬ μŠ€μ½”ν”„**라고 ν•œλ‹€.

`μŠ€μ½”ν”„μ˜ 싀체`λŠ” μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμ˜ `λ ‰μ‹œμ»¬ ν™˜κ²½`이닀. 이 λ ‰μ‹œμ»¬ ν™˜κ²½μ€ μžμ‹ μ˜ "μ™ΈλΆ€ λ ‰μ‹œμ»¬ ν™˜κ²½μ— λŒ€ν•œ μ°Έμ‘°"에 μ €μž₯ν•  참쑰값이 λ°”λ‘œ μƒμœ„ λ ‰μ‹œμ»¬ ν™˜κ²½μ— λŒ€ν•œ 참쑰이며, 이것이 μƒμœ„ μŠ€μ½”ν”„μ΄κΈ° λ•Œλ¬Έμ΄λ‹€.

즉, λ ‰μ‹œμ»¬ μŠ€μ½”ν”„λŠ”
=> `λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ "μ™ΈλΆ€ λ ‰μ‹œμ»¬ ν™˜κ²½μ— λŒ€ν•œ μ°Έμ‘°"에 μ €μž₯ν•  μ°Έμ‘°κ°’, 즉 μƒμœ„ μŠ€μ½”ν”„μ— λŒ€ν•œ μ°Έμ‘°λŠ” ν•¨μˆ˜ μ •μ˜κ°€ ν‰κ°€λ˜λŠ” μ‹œμ μ— ν•¨μˆ˜κ°€ μ •μ˜λœ ν™˜κ²½(μœ„μΉ˜)에 μ˜ν•΄ κ²°μ •.`

```jsx
const x = 1;

function foo() {
const x = 10;
bar();
}

function bar() {
console.log(x);
}

foo(); // ?
bar(); // ?
```

### 2. ν•¨μˆ˜ 객체의 λ‚΄λΆ€ 슬둯 [[Environment]]

---

#### _ν•¨μˆ˜ 객체의 λ‚΄λΆ€ 슬둯 [[Environment]]_

ν•¨μˆ˜κ°€ μ •μ˜λœ ν™˜κ²½(μœ„μΉ˜)κ³Ό ν˜ΈμΆœλ˜λŠ” ν™˜κ²½(μœ„μΉ˜)λŠ” λ‹€λ₯Ό 수 μžˆμ–΄μ„œ λ ‰μ‹œμ»¬ μŠ€μ½”ν”„κ°€ κ°€λŠ₯ν•˜λ €λ©΄ ν•¨μˆ˜λŠ” μžμ‹ μ΄ ν˜ΈμΆœλ˜λŠ” ν™˜κ²½κ³ΌλŠ” 상관없이 μžμ‹ μ΄ μ •μ˜ν•œ ν•¨μˆ˜(=μƒμœ„ μŠ€μ½”ν”„)λ₯Ό κΈ°μ–΅ν•΄μ•Ό ν•œλ‹€. 이λ₯Ό μœ„ν•΄ `ν•¨μˆ˜ μžμ‹ μ˜ λ‚΄λΆ€ 슬둯 [[Environment]]에 μžμ‹ μ΄ μ •μ˜λœ ν™˜κ²½, 즉 μƒμœ„ μŠ€μ½”ν”„λ₯Ό μ €μž₯ν•œλ‹€.`

- μ „μ—­ μ½”λ“œ 평가 μ‹œμ  -> μ „μ—­ λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ μ°Έμ‘° μ €μž₯
- μ™ΈλΆ€ μ½”λ“œ 평가 μ‹œμ  -> μ™ΈλΆ€ λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ μ°Έμ‘° μ €μž₯

ν•¨μˆ˜ 객체의 λ‚΄λΆ€ 슬둯 [[Environment]]에 μ €μž₯된 ν˜„μž¬ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈμ˜ λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ μ°Έμ‘°κ°€ **μƒμœ„ μŠ€μ½”ν”„**λ‹€. λ˜ν•œ μžμ‹ μ΄ 호좜될 λ•Œ 생성될 ν•¨μˆ˜ λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ "μ™ΈλΆ€ λ ‰μ‹œμ»¬ ν™˜κ²½μ— λŒ€ν•œ μ°Έμ‘°"에 μ €μž₯될 μ°Έμ‘° 값이닀. **ν•¨μˆ˜ 객체**λŠ” `λ‚΄λΆ€ 슬둯 [[Environment]]에 μ €μž₯ν•œ λ ‰μ‹œμ»¬ ν™˜κ²½μ˜ μ°Έμ‘°, 즉 μƒμœ„ μŠ€μ½”ν”„λ₯Ό μžμ‹ μ΄ μ‘΄μž¬ν•˜λŠ” ν•œ κΈ°μ–΅ν•œλ‹€.`

### 3. ν΄λ‘œμ €μ™€ λ ‰μ‹œμ»¬ ν™˜κ²½

ν΄λ‘œμ € : μ™ΈλΆ€ ν•¨μˆ˜λ³΄λ‹€ 쀑첩 ν•¨μˆ˜κ°€ 더 였래 μœ μ§€λ˜λŠ” 경우, `이미 생λͺ… μ£ΌκΈ°κ°€ μ’…λ£Œν•œ μ™ΈλΆ€ ν•¨μˆ˜μ˜ λ³€μˆ˜λ₯Ό μ°Έμ‘°ν•  수 μžˆλŠ” 쀑접 ν•¨μˆ˜`. 쀑첩 ν•¨μˆ˜κ°€ μƒμœ„ μŠ€μ½”ν”„μ˜ μ‹λ³„μžλ₯Ό μ°Έμ‘°ν•˜κ³  있고, 쀑첩 ν•¨μˆ˜κ°€ μ™ΈλΆ€ ν•¨μˆ˜λ³΄λ‹€ 더 였래 μœ μ§€λ˜λŠ” κ²½μš°μ—λ§Œ ν•œμ€‘ν•˜λŠ” 것이 일반적.

자유 ν•¨μˆ˜ : ν΄λ‘œμ €μ— μ˜ν•΄ μ°Έμ‘°λ˜λŠ” μƒμœ„ μŠ€μ½”ν”„μ˜ λ³€μˆ˜. ν΄λ‘œμ €λž€ β€œν•¨μˆ˜κ°€ 자유 λ³€μˆ˜μ— λŒ€ν•΄ λ‹«ν˜€μžˆλ‹€.β€λΌλŠ” 의미. 즉, `자유 λ³€μˆ˜μ— λ¬Άμ—¬μžˆλŠ” ν•¨μˆ˜`

---

#### _ν΄λ‘œμ €μ™€ λ ‰μ‹œμ»¬ ν™˜κ²½_

```jsx
const x = 1;

// β‘ 
function outer() {
const x = 10;
const inner = function () { console.log(x); }; // β‘‘
return inner;
}

// outer ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λ©΄ 쀑첩 ν•¨μˆ˜ innerλ₯Ό λ°˜ν™˜ν•œλ‹€.
// 그리고 outer ν•¨μˆ˜μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈλŠ” μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ μŠ€νƒμ—μ„œ νŒλ˜μ–΄ μ œκ±°λœλ‹€.
const innerFunc = outer(); // β‘’
innerFunc(); // β‘£ 10
```

- μƒμœ„ μ˜ˆμ‹œμ—μ„œ, outer ν•¨μˆ˜μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈκ°€ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ μŠ€νƒμ—μ„œ μ œκ±°λ˜λ”λΌλ„ `outer ν•¨μˆ˜μ˜ λ ‰μ‹œμ»¬ ν™˜κ²½μ€ μ†Œλ©Έν•˜μ§€ μ•ŠμŒ`.

- innerλŠ” ν•¨μˆ˜ ν‘œν˜„μ‹μœΌλ‘œ μ •μ˜ν–ˆκΈ° λ•Œλ¬Έμ— λŸ°νƒ€μž„μ— 평가됨. innerν•¨μˆ˜λŠ” [[Environment]] λ‚΄λΆ€ μŠ¬λ‘―μ— ν˜„μž¬ μ‹€ν–‰ 쀑인 μ‹±ν–‰ μ»¨ν…μŠ€νŠΈμ˜ λ ‰μ‹œμ»¬ν™˜κ²½, 즉 outer ν•¨μˆ˜μ˜ λ ‰μ‹œμ»¬ ν™˜κ²½μ„ μƒμœ„ μŠ€μ½”ν”„λ‘œ μ €μž₯.

- outer ν•¨μˆ˜μ˜ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈκ°€ μ‹€ν–‰ μ»¨ν…μŠ€νŠΈ μŠ€νƒμ—μ„œ 제거.

- inner ν•¨μˆ˜μ˜ [[Environment]] λ‚΄λΆ€ μŠ¬λ‘―μ— μ˜ν•΄ 참쑰되고 있고, innter ν•¨μˆ˜λŠ” μ „μ—­ λ³€μˆ˜ innerFunc에 μ˜ν•΄ 참쑰되고 μžˆμœΌλ―€λ‘œ, 가비지 μ»¬λ ‰νŒ…μ΄ λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έ.

- 이둠적으둜 μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λͺ¨λ“  ν•¨μˆ˜λŠ” μƒμœ„ μŠ€μ½”ν”„λ₯Ό κΈ°μ–΅ν•˜κΈ° λ•Œλ¬Έμ— ν΄λ‘œμ €λΌκ³  ν•  수 있음.

- ν•˜μ§€λ§Œ μƒμœ„ μŠ€μ½”ν”„μ˜ μ–΄λ– ν•œ μ‹λ³„μžλ„ μ°Έμ‘°ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ λͺ¨λ˜ λΈŒλΌμš°μ €μ— μ˜ν•΄ μ΅œμ ν™”λ˜μ–΄ μƒμœ„ μŠ€μ½”ν”„λ₯Ό κΈ°μ–΅X.

=> 쀑첩 ν•¨μˆ˜ inner의 λ‚΄λΆ€μ—λŠ” μƒμœ„ μŠ€μ½”ν”„λ₯Ό μ°Έμ‘°ν•  수 μžˆμœΌλ―€λ‘œ μƒμœ„ μŠ€μ½”ν”„μ˜ μ‹λ³„μžλ₯Ό μ°Έμ‘°ν•  μˆ˜λ„, μ‹λ³„μžμ˜ 값을 λ³€κ²½ν•  μˆ˜λ„ 있음.

0 comments on commit 70a2dbe

Please sign in to comment.