Skip to content

Commit

Permalink
Merge pull request #349 from saseungmin/docs/you-dont-know-js
Browse files Browse the repository at this point in the history
  • Loading branch information
saseungmin authored Jan 9, 2023
2 parents f6d7e38 + e1124f4 commit 4d30bbd
Show file tree
Hide file tree
Showing 10 changed files with 3,374 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
|[μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ”©μ˜ 기술](https://saseungmin.github.io/reading_books_record_repository/docs/javascript/modern-javascript/table-of-contents)|[Node.js κ΅κ³Όμ„œ](https://github.com/saseungmin/Node.js-tutorial)|[λ¦¬μ•‘νŠΈλ₯Ό λ‹€λ£¨λŠ” 기술](https://github.com/saseungmin/react-tutorial)|
|:---:|:---:|:---:|
|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/javascript/modern-javascript/table-of-contents"><img src="images/modern-javascript.jpeg" width="400px" height="300px"/></a>|<a href="https://github.com/saseungmin/Node.js-tutorial"><img src="images/node-js-textbook.jpeg" width="400px" height="300px"/></a>|<a href="https://github.com/saseungmin/react-tutorial"><img src="images/react-book.jpeg" width="400px" height="300px"/></a>|
|[**You Don’t Know JS νƒ€μž…κ³Ό 문법, μŠ€μ½”ν”„μ™€ ν΄λ‘œμ €**](https://github.com/saseungmin/reading_books_record_repository/tree/master/summarize_books_in_markdown/You%20Don%E2%80%99t%20Know%20JS%201)|[**Do it! νƒ€μž…μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž˜λ°**](https://saseungmin.github.io/reading_books_record_repository/docs/typescript/do-it-typescript/table-of-contents)|[**ν”„λ‘œ Git 2판: 그림으둜 μ΄ν•΄ν•˜λŠ” Git의 μž‘λ™ 원리와 μ‚¬μš©λ²•**](https://saseungmin.github.io/reading_books_record_repository/docs/git/pro-git-2/table-of-contents)|
|<a href="https://github.com/saseungmin/reading_books_record_repository/tree/master/summarize_books_in_markdown/You%20Don%E2%80%99t%20Know%20JS%201"><img src="images/you-dont-know-js-1.jpeg" width="400px" height="300px"/></a>|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/typescript/do-it-typescript/table-of-contents"><img src="images/do-it-typescript.jpeg" width="400px" height="300px"/></a>|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/git/pro-git-2/table-of-contents"><img src="images/pro-git-2e.jpeg" width="400px" height="300px"/></a>|
|[**You Don’t Know JS νƒ€μž…κ³Ό 문법, μŠ€μ½”ν”„μ™€ ν΄λ‘œμ €**](https://saseungmin.github.io/reading_books_record_repository/docs/javascript/you-dont-know-js/table-of-contents)|[**Do it! νƒ€μž…μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž˜λ°**](https://saseungmin.github.io/reading_books_record_repository/docs/typescript/do-it-typescript/table-of-contents)|[**ν”„λ‘œ Git 2판: 그림으둜 μ΄ν•΄ν•˜λŠ” Git의 μž‘λ™ 원리와 μ‚¬μš©λ²•**](https://saseungmin.github.io/reading_books_record_repository/docs/git/pro-git-2/table-of-contents)|
|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/javascript/you-dont-know-js/table-of-contents"><img src="images/you-dont-know-js-1.jpeg" width="400px" height="300px"/></a>|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/typescript/do-it-typescript/table-of-contents"><img src="images/do-it-typescript.jpeg" width="400px" height="300px"/></a>|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/git/pro-git-2/table-of-contents"><img src="images/pro-git-2e.jpeg" width="400px" height="300px"/></a>|
|[**μ½”μ–΄ μžλ°”μŠ€ν¬λ¦½νŠΈ**](https://saseungmin.github.io/reading_books_record_repository/docs/javascript/core-javascript/table-of-contents)|[**μ‰½κ²Œ λ°°μš°λŠ” μ•Œκ³ λ¦¬μ¦˜**](https://github.com/saseungmin/reading_books_record_repository/tree/master/summarize_books_in_markdown/%EC%89%BD%EA%B2%8C%20%EB%B0%B0%EC%9A%B0%EB%8A%94%20%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98)|[**ν•¨κ»˜ 자라기**](https://saseungmin.github.io/reading_books_record_repository/docs/agile/growing-up-together/table-of-contents)|
|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/javascript/core-javascript/table-of-contents"><img src="images/core-javascript.jpeg" width="400px" height="300px"/></a>|<a href="https://github.com/saseungmin/reading_books_record_repository/tree/master/summarize_books_in_markdown/%EC%89%BD%EA%B2%8C%20%EB%B0%B0%EC%9A%B0%EB%8A%94%20%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98"><img src="images/algorism-book.jpeg" width="400px" height="300px"/></a>|<a href="https://saseungmin.github.io/reading_books_record_repository/docs/agile/growing-up-together/table-of-contents"><img src="images/thinking-together.jpeg" width="400px" height="300px"/></a>|
|[**μ‹€μš©μ£Όμ˜ ν”„λ‘œκ·Έλž˜λ¨Έ**](https://saseungmin.github.io/reading_books_record_repository/docs/etc/pragmatic-programmer/table-of-contents)|[**μΈμŠ€νŒŒμ΄μ–΄λ“œ**](https://saseungmin.github.io/reading_books_record_repository/docs/etc/inspired/table-of-contents)|[**ν…ŒμŠ€νŠΈ 주도 개발**](https://saseungmin.github.io/reading_books_record_repository/docs/test/test-driven-development/table-of-contents)|
Expand Down
4 changes: 4 additions & 0 deletions docs/javascript/table-of-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ sidebar_position: 1
### πŸ‘‰ [μžλ°”μŠ€ν¬λ¦½νŠΈ νŒ¨ν„΄κ³Ό ν…ŒμŠ€νŠΈ](/docs/javascript/javascript-pattern-and-test/table-of-contents)
- [λ„μ„œ 정보](http://www.yes24.com/Product/Goods/33211518)
- μ €: 래리 μŠ€νŽœμ„œ, μ„ΈμŠ€ 리처즈

### πŸ‘‰ [You Don’t Know JS νƒ€μž…κ³Ό 문법, μŠ€μ½”ν”„μ™€ ν΄λ‘œμ €](/docs/javascript/you-dont-know-js/table-of-contents)
- [λ„μ„œ 정보](http://www.yes24.com/Product/Goods/43219481)
- μ €: 카일 μ‹¬μŠ¨
4 changes: 4 additions & 0 deletions docs/javascript/you-dont-know-js/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"label": "You don't know JS: νƒ€μž…κ³Ό 문법, μŠ€μ½”ν”„μ™€ ν΄λ‘œμ €",
"position": 5
}
236 changes: 236 additions & 0 deletions docs/javascript/you-dont-know-js/chapter-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
---
sidebar_position: 2
---

# 🌈 Chapter 1 : νƒ€μž…

<details><summary>Table of Contents</summary>

- νƒ€μž…, κ·Έ 싀체λ₯Ό μ΄ν•΄ν•˜μž. [:link:](#-νƒ€μž…-κ·Έ-싀체λ₯Ό-μ΄ν•΄ν•˜μž)
- λ‚΄μž₯ νƒ€μž… [:link:](#-λ‚΄μž₯-νƒ€μž…)
- 값은 νƒ€μž…μ„ 가진닀 [:link:](#-값은-νƒ€μž…μ„-가진닀)
- 값이 μ—†λŠ” vs μ„ μ–Έλ˜μ§€ μ•Šμ€ [:link:](#-값이-μ—†λŠ”-vs-μ„ μ–Έλ˜μ§€-μ•Šμ€)
- μ„ μ–Έλ˜μ§€ μ•Šμ€ λ³€μˆ˜ [:link:](#-μ„ μ–Έλ˜μ§€-μ•Šμ€-λ³€μˆ˜)
- μ •λ¦¬ν•˜κΈ° [:link:](#-μ •λ¦¬ν•˜κΈ°)

</details>

- [ESMA ν‘œμ€€ λͺ…μ„Έμ„œ 5.1](https://www.ecma-international.org/ecma-262/5.1/)λ₯Ό ν™•μΈν•΄λ³΄μž.
- 이 λͺ…세에 수둝된 μ•Œκ³ λ¦¬μ¦˜μ—μ„œ μ‚¬μš©λ˜λŠ” λͺ¨λ“  값은 이 μ ˆμ—μ„œ μ •μ˜ν•œ νƒ€μž… λͺ©λ‘μ€‘ ν•˜λ‚˜μ— ν•΄λ‹Ήν•œλ‹€.
- νƒ€μž…μ€ ECMAScript **μ–Έμ–΄ νƒ€μž…**κ³Ό **λͺ…μ„Έ νƒ€μž…**으둜 ν•˜μœ„ λΆ„λ₯˜λœλ‹€.
- ECMAScript κ°œλ°œμžκ°€ ECMAScript μ–Έμ–΄λ₯Ό μ΄μš©ν•˜μ—¬ 직접 μ‘°μž‘ν•˜λŠ” κ°’λ“€μ˜ νƒ€μž…μ΄ λ°”λ‘œ ECMAScript μ–Έμ–΄ νƒ€μž…μ΄λ‹€.
- ECMAScript μ–Έμ–΄ νƒ€μž…μ—λŠ” `Undefined`, `null`, `Boolean`, `String`, `Number`, `Object`κ°€ μžˆλ‹€.

## 🎯 νƒ€μž…, κ·Έ 싀체λ₯Ό μ΄ν•΄ν•˜μž.
- νƒ€μž…λ³„λ‘œ λ‚΄μž¬λœ νŠΉμ„±μ„ μ œλŒ€λ‘œ μ•Œκ³  μžˆμ–΄μ•Ό 값을 λ‹€λ₯Έ νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•˜λŠ” 방법을 μ •ν™•νžˆ 이해할 수 μžˆλ‹€.
- μ–΄λ–€ ν˜•νƒœλ‘œλ“  거의 λͺ¨λ“  μžλ°”μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž¨μ—μ„œ **κ°•μ œλ³€ν™˜(coercion)이 μΌμ–΄λ‚˜λ―€λ‘œ νƒ€μž…μ„ ν™•μ‹€ν•˜κ²Œ μΈμ§€ν•˜κ³  μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.**
> `42`λ₯Ό λ¬Έμžμ—΄λ‘œ 보고 μœ„μΉ˜ 1μ—μ„œ `"2"`λΌλŠ” 문자λ₯Ό μΆ”μΆœν•˜λ €λ©΄, λ¨Όμ € 숫자 `42` -> λ¬Έμžμ—΄ `"42"`둜 λ³€κ²½(κ°•μ œλ³€ν™˜)ν•΄μ•Ό ν•œλ‹€.
## 🎯 λ‚΄μž₯ νƒ€μž…
- μžλ°”μŠ€ν¬λ¦½νŠΈμ—λŠ” λ‹€μŒ 7가지 λ‚΄μž₯ νƒ€μž…μ΄ μžˆλ‹€.

```
null, undefined, boolean, number, string, object, symbol(ES6λΆ€ν„° μΆ”κ°€)
```
- `object`λ₯Ό μ œμ™Έν•œ 이듀은 **μ›μ‹œ νƒ€μž…(Primitives)** 이닀.
- κ°’ νƒ€μž…μ€ typeof μ—°μ‚°μžλ‘œ μ•Œ 수 μžˆλ‹€.
- ν•˜μ§€λ§Œ typeof λ°˜ν™˜ 값은 항상 7가지 λ‚΄μž₯ νƒ€μž… 쀑 ν•˜λ‚˜λŠ” μ•„λ‹ˆλ‹€.
- 7가지 λ‚΄μž₯ νƒ€μž…κ³Ό 1:1둜 μ •ν™•νžˆλŠ” λ§€μΉ˜λ˜μ§€ μ•ŠλŠ”λ‹€.

```javascript
typeof undefined === 'undefined'; // true
typeof true === 'boolean'; // true
typeof 42 === 'number'; // true
typeof "42" === 'string' // true
typeof { life: 42 } === 'object'; // true

// ES6λΆ€ν„° μΆ”κ°€
typeof Symbol() === 'symbol'; // true
```
- 예제의 6개 νƒ€μž…μ€ μžμ‹ μ˜ λͺ…μΉ­κ³Ό λ™μΌν•œ λ¬Έμžμ—΄μ„ λ°˜ν™˜ν•œλ‹€.
- `null`에 λŒ€ν•œ `typeof` μ—°μ‚° κ²°κ³ΌλŠ” κΌ­ λ²„κ·Έμ²˜λŸΌ 보인닀.

```javascript
typeof null === 'object'; // true
typeof null === 'null'; // false
```
- λ²„κ·Έμ²˜λŸΌ λ³΄μ΄μ§€λ§Œ 이게 μ •λ‹΅. (거의 20λ…„ λ™μ•ˆ 이 λ²„κ·ΈλŠ” 이제 μ™€μ„œ 손을 λŒ€μžλ‹ˆ λ‹€λ₯Έ 버그가 생겨 잘 λŒμ•„κ°€λ˜ μ›Ή μ†Œν”„νŠΈμ›¨μ–΄κ°€ λ©ˆμΆ°λ²„λ¦΄ κ²½μš°κ°€ λ§Žμ•„μ„œ μ•žμœΌλ‘œλ„ 해결될 κ°€λŠ₯성은 μ—†λ‹€.)
- κ·Έλž˜μ„œ νƒ€μž…μœΌλ‘œ `null` 값을 μ •ν™•νžˆ ν™•μΈν•˜λ €λ©΄ 쑰건이 ν•˜λ‚˜ 더 ν•„μš”ν•˜λ‹€.

```javascript
const a = null;
(!a && typeof a === 'object'); // true
```
- `null`은 falsyν•œ μœ μΌν•œ μ›μ‹œ κ°’μ΄μ§€λ§Œ, νƒ€μž…μ€ `object`인 νŠΉλ³„ν•œ μ‘΄μž¬μ΄λ‹€.
- λ˜ν•œ `typeof`κ°€ λ°˜ν™˜ν•˜λŠ” λ¬Έμžμ—΄μ€ ν•˜λ‚˜ 더 μ‘΄μž¬ν•œλ‹€.

```javascript
typeof function a(){ /* ... */ } === "function"; // true
typeof function a(){ /* ... */ } === "object"; // false
```
- `typeof` λ°˜ν™˜ 값을 보면 마치 `function`이 μ΅œμƒμœ„ 레벨의 λ‚΄μž₯ νƒ€μž…μ²˜λŸΌ λ³΄μ΄μ§€λ§Œ λͺ…μ„Έλ₯Ό 읽어보면 **μ‹€μ œλ‘œλŠ” `object`의 'ν•˜μœ„ νƒ€μž…'이닀.**
- ꡬ체적으둜 ν•¨μˆ˜λŠ” '**호좜 κ°€λŠ₯ν•œ 객체(Callable Object)**'라고 λͺ…μ‹œλ˜μ–΄ μžˆλ‹€.
- κ·Έλ ‡κΈ° λ•Œλ¬Έμ— ν•¨μˆ˜λŠ” κ°μ²΄λΌμ„œ μœ μš©ν•˜λ‹€. 무엇보닀 **ν•¨μˆ˜μ— ν”„λ‘œνΌν‹°λ₯Ό λ‘˜ 수 μžˆλ‹€.**

```javascript
function a(b, c) {
/* ... */
}
```
- ν•¨μˆ˜μ— μ„ μ–Έλœ 인자 κ°œμˆ˜λŠ” ν•¨μˆ˜ 객체의 `length` ν”„λ‘œνΌν‹°λ‘œ μ•Œ 수 μžˆλ‹€.

```javascript
a.length; // 2
```

- ν•¨μˆ˜ aλŠ” 인자 두 개(b, c)λ₯Ό κ°€μ§€λ―€λ‘œ 'ν•¨μˆ˜μ˜ 길이'λŠ” 2λ‹€.
- **배열도 κ·Έλƒ₯ 객체이닀.**

```javascript
typeof [1, 2, 3] === 'object'; // true
```
- 배열은 숫자 인덱슀λ₯Ό 가지며, `length` ν”„λ‘œνΌν‹°κ°€ μžλ™μœΌλ‘œ κ΄€λ¦¬λ˜λŠ” λ“±μ˜ μΆ”κ°€ νŠΉμ„±μ„ μ§€λ‹Œ, 객체의 'ν•˜μœ„ νƒ€μž…'이라 ν•  수 μžˆλ”°.

## 🎯 값은 νƒ€μž…μ„ 가진닀.
- κ°’μ—λŠ” νƒ€μž…μ΄ μžˆμ§€λ§Œ, **λ³€μˆ˜μ—” λ”°λ‘œ νƒ€μž…μ΄λž€ μ—†λ‹€.**
- λ³€μˆ˜λŠ” μ–Έμ œλΌλ„, **μ–΄λ–€ ν˜•νƒœμ˜ 값이라도 κ°€μ§ˆ 수 μžˆλ‹€.**
- μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” **νƒ€μž… κ°•μ œ(Type Enforcement)** λ₯Ό ν•˜μ§€ μ•ŠλŠ”λ‹€.
- λ³€μˆ«κ°’μ— μ²˜μŒμ— ν• λ‹Ήλœ κ°’κ³Ό λ™μΌν•œ νƒ€μž…μΌ ν•„μš”λŠ” μ—†λ‹€. (λ¬Έμžμ—΄μ„ λ„£μ—ˆλ”°κ°€ λ‚˜μ€‘μ— 숫자λ₯Ό 넣어도 상관없닀.)

```javascript
var a = 42;
typeof a; // "number"
a = true;
typeof a; // "boolean"
```
- `typeof` μ—°μ‚°μžμ˜ λ°˜ν™˜ 값은 **μ–Έμ œλ‚˜ λ¬Έμžμ—΄μ΄λ‹€.**

```javascript
typeof typeof 42; // "string"
```
- λ”°λΌμ„œ `typeof 42`λŠ” `"number"`λ₯Ό λ°˜ν™˜ν•˜κ³ , `typeof "number"`의 결과값은 `"string"`이닀.

### πŸ“š 값이 μ—†λŠ” vs μ„ μ–Έλ˜μ§€ μ•Šμ€
- 값이 μ—†λŠ” λ³€μˆ˜μ˜ 값은 `undefinded`이며, `typeof` κ²°κ³ΌλŠ” `"undefined"`λ‹€.

```javascript
var a;
typeof a; // "undefined"

var b = 42;
var c;

b = c;

typeof b; // "undefined"
typeof c; // "undefined"
```
- `undefined`와 `undeclared`λ₯Ό λ™μ˜μ–΄μ²˜λŸΌ μƒκ°ν•˜κΈ° μ‰½μ§€λ§Œ, μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ λ‘˜μ€ μ™„μ „νžˆ λ‹€λ₯Έ κ°œλ…μ΄λ‹€.
- `undefined`λŠ” **μ ‘κ·Ό κ°€λŠ₯ν•œ μŠ€μ½”ν”„μ— λ³€μˆ˜κ°€ μ„ μ–Έλ˜μ—ˆμœΌλ‚˜ ν˜„μž¬ μ•„λ¬΄λŸ° 값도 ν• λ‹Ήλ˜μ§€ μ•Šμ€ μƒνƒœλ₯Ό 가리킨닀.**
- `undeclared`λŠ” **μ ‘κ·Ό κ°€λŠ₯ν•œ μŠ€μ½”ν”„μ— λ³€μˆ˜ μžμ²΄κ°€ μ„ μ–Έμ‘°μ°¨ λ˜μ§€ μ•Šμ€ μƒνƒœμ΄λ‹€.**

```javascript
var a;

a; // "undefined"
b; // Uncaught ReferenceError: b is not defined
```
- μ—¬κΈ°μ„œ `b is not defined` μ—λŸ¬ λ©”μ‹œμ§€κ°€ `undefined`이라고 생각할 수 μžˆμ§€λ§Œ, `is not defined`와 `undefined`λŠ” **μ˜λ―Έκ°€ μ™„μ „νžˆ λ‹€λ₯΄λ‹€.**
- μ„ μ–Έλ˜μ§€ μ•Šμ€(undefined) λ³€μˆ˜μ˜ `typeof` μ—°μ‚° κ²°κ³ΌλŠ” 더 ν—·κ°ˆλ¦°λ‹€.

```javascript
var a;

typeof a; // "undefined"
typeof b; // "undefined"
```
- μ„ μ–Έλ˜μ§€ μ•Šμ€ `typeof`ν•˜λ©΄ `"undefined"`둜 λ‚˜μ˜¨λ‹€. `b`λŠ” λΆ„λͺ… μ„ μ–Έμ‘°μ°¨ ν•˜μ§€ μ•Šμ€ λ³€μˆ˜μΈλ° `typeof b`λ₯Ό 해도 λΈŒλΌμš°μ €λŠ” 였λ₯˜ 처리λ₯Ό ν•˜μ§€ μ•ŠλŠ”λ‹€.
- λ°”λ‘œ 이것이 `typeof`만의 λ…νŠΉν•œ **μ•ˆμ „ κ°€λ“œ(safety guard)** 이닀.


### πŸ“š μ„ μ–Έλ˜μ§€ μ•Šμ€ λ³€μˆ˜
- λΈŒλΌμš°μ €μ—μ„œ μžλ°”μŠ€ν¬λ¦½νŠΈ μ½”λ“œλ₯Ό μ²˜λ¦¬ν•  λ•Œ, 특히 μ—¬λŸ¬ 슀크립트 파일의 λ³€μˆ˜λ“€μ΄ μ „μ—­ λ„€μž„μŠ€νŽ˜μ΄μŠ€λ₯Ό κ³΅μœ ν•  λ•Œ, `typeof`의 μ•ˆμ „ κ°€λ“œλŠ” μ“Έλͺ¨κ°€ μžˆλ‹€.

```javascript
// debug.js
var DEBUG = true;

// λ‹€λ₯Έ 파일
// μ—λŸ¬ λ°œμƒ
if(DEBUG) {
console.log('디버깅 μ‹œμž‘');
}

// μ•„λž˜μ™€ 같이 μ•ˆμ „ν•˜κ²Œ 쑴재 μ—¬λΆ€ 체크 κ°€λŠ₯ (μ „μ—­ λ³€μˆ˜ 체크)
if(typeof DEBUG !== 'undefined') {
console.log('디버그 μ‹œμž‘');
}
```

- `typeof` μ•ˆμ „ κ°€λ“œ 없이 μ „μ—­ λ³€μˆ˜λ₯Ό μ²΄ν¬ν•˜λŠ” λ‹€λ₯Έ 방법은 μ „μ—­ λ³€μˆ˜κ°€ λͺ¨λ‘ μ „μ—­ 객체(λΈŒλΌμš°μ €λŠ” window)의 ν”„λ‘œνΌν‹°λΌλŠ” 점을 μ΄μš©ν•œλ‹€λŠ” 점이닀.
- κ·Έλž˜μ„œ λ‹€μŒκ³Ό 같이 체크할 수 μžˆλ‹€.

```javascript
if(window.DEBUG) {
// ...
}
if (window.atob) {
// ...
}
```
- μ„ μ–Έλ˜μ§€ μ•Šμ€ λ³€μˆ˜ λ•Œμ™€ 달리 μ–΄λ–€ 객체의 ν”„λ‘œνΌν‹°λ₯Ό μ ‘κ·Όν•  λ•Œ κ·Έ ν”„λ‘œνΌν‹°κ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ•„λ„ `ReferenceError`κ°€ λ‚˜μ§€λŠ” μ•ŠλŠ”λ‹€.
- ν•˜μ§€λ§Œ window 객체λ₯Ό ν†΅ν•œ μ „μ—­ λ³€μˆ˜ μ°Έμ‘°λŠ” 가급적 μ‚Όκ°€λŠ” 것이 μ’‹λ‹€.
- μ—„λ°€νžˆ λ§ν•΄μ„œ `typeof` μ•ˆμ „ κ°€λ“œλŠ” μ „μ—­ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šμ„ λ•Œμ—λ„ μœ μš©ν•œλ°, 일뢀 κ°œλ°œμžλ“€μ€ 이런 섀계 방식이 그닀지 λ°”λžŒμ§ν•˜μ§€ μ•Šλ‹€κ³  λ§ν•œλ‹€.
- 예λ₯Ό λ“€μ–΄ λ‹€λ₯Έ κ°œλ°œμžκ°€ μž‘μ„±ν•œ μœ ν‹Έλ¦¬ν‹° ν•¨μˆ˜λ₯Ό μžμ‹ μ˜ λͺ¨λ“ˆ/ν”„λ‘œκ·Έλž¨μ— μΉ΄ν”Ό μ•€ νŽ˜μ΄μŠ€νŠΈν•˜μ—¬ μ‚¬μš©ν•˜λŠ”λ°, κ°€μ Έλ‹€ μ“°λŠ” ν”„λ‘œκ·Έλž¨μ— μœ ν‹Έλ¦¬ν‹°μ˜ νŠΉμ • λ³€μˆ«κ°’μ΄ μ •μ˜λ˜μ–΄ μžˆλŠ”μ§€ 체크해야 ν•˜λŠ” 상황을 κ°€μ‘ν•΄λ³΄μž.

```javascript
function doSomethingCool() {
var helper =
(typeof FeatureXYZ !== 'undefinded') ?
FeatureXYZ :
function() { /*... κΈ°λ³Έ XYZ κΈ°λŠ₯ */}

var val = helper();
// ...
}
```
- `doSomethingCool` ν•¨μˆ˜λŠ” `FeatureXYZ` λ³€μˆ˜κ°€ μ‘΄μž¬ν•˜λ©΄ κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜κ³  μ—†μœΌλ©΄ ν•¨μˆ˜ λ°”λ””λ₯Ό μ •μ˜ν•˜λŠ”λ°, μ΄λ ‡κ²Œ ν•΄μ•Ό λ‹€λ₯Έ μ‚¬λžŒμ΄ μΉ΄ν”Ό μ•€ νŽ˜μ΄μŠ€ν‹€ 해도 μ•ˆμ „ν•˜κ²Œ `FeatureXYZ`κ°€ μ‘΄μž¬ν•˜λŠ”μ§€λ₯Ό 체크할 수 μžˆλ‹€.

```javascript
(function() {
function FeatureXYZ() { /* μœ ν‹Έ XYZ κΈ°λŠ₯ */}

function doSomethingCool() {
var helper =
(typeof FeatureXYZ !== 'undefinded') ?
FeatureXYZ :
function() { /*... κΈ°λ³Έ XYZ κΈ°λŠ₯ */}

var val = helper();
// ...
}

doSomethingCool();
})();
```
- `FeatureXYZ`λŠ” μ „μ—­ λ³€μˆ˜κ°€ μ•„λ‹ˆμ§€λ§Œ, `typeof` μ•ˆμ „ κ°€λ“œλ₯Ό μ΄μš©ν•˜μ—¬ μ•ˆμ „ν•˜κ²Œ μ²΄ν¬ν•˜κ³  μžˆλ‹€.
- λ˜ν•œ, **μ˜μ‘΄μ„± μ£Όμž…(Dependency Injection)** 섀계 νŒ¨ν„΄μœΌλ‘œ `FeatureXYZ`κ°€ `doSomethingCool` ν•¨μˆ˜μ˜ λ°”κΉ₯μ΄λ‚˜ 언저리에 μ •μ˜λ˜μ—ˆλŠ”μ§€ μ•”μ‹œμ μœΌλ‘œ μ‘°μ‚¬ν•˜λŠ” λŒ€μ‹ , λ‹€μŒ μ½”λ“œμ²˜λŸΌ λͺ…μ‹œμ μœΌλ‘œ 의쑴 관계λ₯Ό 전달할 수 μžˆλ‹€.

```javascript
function doSomethingCool(FeatureXYZ) {
var helper =
(typeof FeatureXYZ !== 'undefinded') ?
FeatureXYZ :
function() { /*... κΈ°λ³Έ XYZ κΈ°λŠ₯ */}

var val = helper();
// ...
}
```

## 🎯 μ •λ¦¬ν•˜κΈ°
- μžλ°”μŠ€ν¬λ¦½νŠΈμ—λŠ” 7가지 λ‚΄μž₯ νƒ€μž…(`null`, `undefined`, `boolean`, `number`, `string`, `object`, `symbol`)이 있으며, `typeof` μ—°μ‚°μžλ‘œ νƒ€μž…λͺ…을 μ•Œμ•„λ‚Έλ‹€.
- λ³€μˆ˜λŠ” νƒ€μž…μ΄ μ—†μ§€λ§Œ 값은 νƒ€μž…μ΄ 있고, νƒ€μž…μ€ κ°’μ˜ λ‚΄μž¬λœ νŠΉμ„±μ„ μ •μ˜ν•œλ‹€.
- μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ `undefined`와 `undeclared`λŠ” μ „ν˜€ λ‹€λ₯΄κ²Œ μ·¨κΈ‰ν•˜λŠ”λ° `undefined`λŠ” μ„ μ–Έλœ λ³€μˆ˜μ— ν• λ‹Ήν•  수 μžˆλŠ” κ°’μ΄μ§€λ§Œ, `undeclared`λŠ” λ³€μˆ˜ μžμ²΄κ°€ μ„ μ–Έλœ 적이 μ—†μŒμ„ λ‚˜νƒ€λ‚Έλ‹€.
- λΆˆν–‰νžˆλ„ μžλ°”μŠ€ν¬λ¦½νŠΈλ₯Ό 두 μš©μ–΄λ₯Ό λŒ€μΆ© μ„žμ–΄λ²„λ €, μ—λŸ¬ λ©”μ‹œμ§€ 뿐만 μ•„λ‹ˆλΌ `typeof` λ°˜ν™˜ 값도 λͺ¨λ‘ `undefined`둜 λ­‰λš±κ·Έλ¦°λ‹€.
- κ·Έλž˜λ„ `typeof` μ•ˆμ „ κ°€λ“œ 덕뢄에 μ„ μ–Έλ˜μ§€ μ•Šμ€ λ³€μˆ˜μ— μ‚¬μš©ν•˜λ©΄ μ“Έλ§Œν•΄μ§„λ‹€.
Loading

0 comments on commit 4d30bbd

Please sign in to comment.