Primitive Types & Reference Types in JavaScript - Lee-hyuna/33-js-concepts-kr GitHub Wiki

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ κΈ°λ³Έ νƒ€μž… & μ°Έμ‘° νƒ€μž…

λ‹€λ₯Έ μ–Έμ–΄λ“€κ³ΌλŠ” λ‹€λ₯Έ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ pass-by-value 에 λŒ€ν•œ μ„€λͺ…μž…λ‹ˆλ‹€.

Facts

  • μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” μ›μ‹œμ™€ μ°Έμ‘° 2 μ’…λ₯˜μ˜ νƒ€μž…μ„ 가지고 μžˆλ‹€.
  • λ³€μˆ˜κ°€ 볡사 λ˜μ—ˆμ„ λ•Œ, λ©”λͺ¨λ¦¬ μ•ˆμ˜ 값이 λ³΅μ‚¬λœλ‹€.
  • λͺ¨λ“  λ³€μˆ˜κ°€ μƒμ„±λœ 후에 κ³ μ •λœ μ–‘μ˜ λ©”λͺ¨λ¦¬κ°€ ν• λ‹Ήλ©λ‹ˆλ‹€.
  • λ³€μˆ˜κ°€ 볡사 될 λ•Œ ,λ©”λͺ¨λ¦¬ λ‚΄μ˜ 값이 λ³΅μ‚¬λ©λ‹ˆλ‹€.

Primitive Types

λ©”λͺ¨λ¦¬μ•ˆμ˜ μ›μ‹œ νƒ€μž…μ€ μ‹€μ œ κ°’(e.g. boolean true, number 42) μž…λ‹ˆλ‹€. μ›μ‹œ νƒ€μž…μ€ κ³ μ •λœ μ–‘μ˜ λ©”λͺ¨λ¦¬μ— μ €μž₯ 될 수 μžˆμŠ΅λ‹ˆλ‹€.

  • null
  • undefined
  • Boolean
  • Number
  • String

μ›μ‹œ νƒ€μž…μ€ 슀칼라 λ˜λŠ” λ‹¨μˆœν•œ νƒ€μž…μ΄λΌκ³ λ„ ν•œλ‹€.

Reference Types

μ°Έμ‘° μœ ν˜•μ€ λ‹€λ₯Έ 값듀을 포함 ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ°Έμ‘° μœ ν˜•μ˜ λ‚΄μš©μ€ λ³€μˆ˜μ— 이용될 수 μžˆλŠ” κ³ μ •λœ λ©”λͺ¨λ¦¬ 양에 λ§žμ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€. μ°Έμ‘° μœ ν˜•μ˜ λ©”λͺ¨λ¦¬μ•ˆμ˜ 값은 μ°Έμ‘° μœ ν˜• 자체의 κ°’ μž…λ‹ˆλ‹€.

  • Array
  • Object
  • Function

μ°Έμ‘° μœ ν˜•μ€ 볡합, μ»¨ν…Œμ΄λ„ˆ μœ ν˜•μ΄λΌκ³ λ„ ν•©λ‹ˆλ‹€.

Code Examples

Copying a primitive:

var a = 13         // assign `13` to `a`
var b = a          // copy the value of `a` to `b`
b = 37             // assign `37` to `b`
console.log(a)     // => 13

원본 값은 λ°”λ€Œμ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μš°λ¦¬λŠ” 였직 λ³€κ²½λœ κ°’λ§Œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€.

Copying a reference:

var a = { c: 13 }  // assign the reference of a new object to `a`
var b = a          // copy the reference of the object inside `a` to new variable `b`
b.c = 37           // modify the contents of the object `b` refers to
console.log(a)     // => { c: 37 }

μ°Έμ‘°κ°€ λ³΅μ‚¬λœ ν›„ 원본도 λ³€κ²½λ©λ‹ˆλ‹€.

⚠️ **GitHub.com Fallback** ⚠️