-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #349 from saseungmin/docs/you-dont-know-js
- Loading branch information
Showing
10 changed files
with
3,374 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"label": "You don't know JS: νμ κ³Ό λ¬Έλ², μ€μ½νμ ν΄λ‘μ ", | ||
"position": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` μμ κ°λ λλΆμ μ μΈλμ§ μμ λ³μμ μ¬μ©νλ©΄ μΈλ§ν΄μ§λ€. |
Oops, something went wrong.