πŸ‘¨πŸ»β€πŸ’» λͺ¨λ˜ λ¦¬μ•‘νŠΈ Deep Dive

pjw__98Β·2023λ…„ 12μ›” 5일
1
post-thumbnail

πŸ“† λ‚ μ§œ : 2023/12/05(ν™”)

πŸ“š 챕터 : μ›μ‹œνƒ€μž…κ³Ό κ°μ²΄νƒ€μž…μ˜ νŠΉμ§• 및
값을 μ €μž₯ν•˜λŠ” λ°©μ‹μ˜ 차이


🎯 ν•™μŠ΅ λ‚΄μš©

μ›μ‹œ νƒ€μž…κ³Ό 객체 νƒ€μž…μ˜ κ°€μž₯ 큰 차이점은 값을 μ €μž₯ν•˜λŠ” 방식이닀.
값을 μ–΄λ–»κ²Œ μ €μž₯ν•˜λƒμ— 따라 동등 비ꡐλ₯Ό ν•  λ•Œ 차이λ₯Ό λ§Œλ“ λ‹€.
λ¨Όμ € μ›μ‹œ νƒ€μž…μ€ λΆˆλ³€ ν˜•νƒœμ˜ κ°’μœΌλ‘œ μ €μž₯λœλ‹€.
이 값은 λ³€μˆ˜ ν• λ‹Ή μ‹œμ μ— λ©”λͺ¨λ¦¬ μ˜μ—­μ„ μ°¨μ§€ν•˜κ³  μ €μž₯λœλ‹€.
μ•„λž˜ 예제 μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄μž

<script>
let hello = 'hello world'
let hi = hello

console.log(hello === hi) // true
</script>

λ‹Ήμ—°νžˆ 두 값을 λΉ„κ΅ν•˜λ©΄ trueκ°€ λ°˜ν™˜λœλ‹€.
hello λ³€μˆ˜μ˜ hello world 값이 hiλΌλŠ” λ³€μˆ˜μ— hello κ°’ 자체λ₯Ό ν• λ‹Ήν•¨μœΌλ‘œμ¨ 볡사해 전달됐기 λ•Œλ¬Έμ΄λ‹€.

반면 κ°μ²΄λŠ” ν”„λ‘œνΌν‹°λ₯Ό μΆ”κ°€,μˆ˜μ •,μ‚­μ œκ°€ κ°€λŠ₯ ν•˜λ―€λ‘œ μ›μ‹œ κ°’κ³Ό λ‹€λ₯΄κ²Œ
λ³€κ²½ κ°€λŠ₯ν•œ ν˜•νƒœλ‘œ μ €μž₯되며, 값을 볡사할 λ•Œλ„ 값이 μ•„λ‹Œ μ°Έμ‘°λ₯Ό μ „λ‹¬ν•˜κ²Œ λœλ‹€.
μ•„λž˜ 예제 μ½”λ“œλ₯Ό μ‚΄νŽ΄λ³΄μž

<script>

var hello = {greet : 'hello world',}

var hi = {greet : 'hello world',}

console.log(hello === hi); //false

console.log(hello.greet === hi.greet);//true

</script>

μ›μ‹œνƒ€μž…μ€ 값을 μ €μž₯ν•œλ‹€λ©΄ κ°μ²΄λŠ” 값을 μ €μž₯ν•˜λŠ”κ²Œ μ•„λ‹Œ μ°Έμ‘°λ₯Ό μ €μž₯ν•˜κΈ° λ•Œλ¬Έμ—
hello와 hi을 λ™λ“±λΉ„κ΅ν–ˆμ„ λ•Œ falseλ₯Ό λ°˜ν™˜ν•œλ‹€.
값은 κ°™μ§€λ§Œ μ°Έμ‘°ν•˜λŠ” 곳이 λ‹€λ₯Έ μ…ˆμ΄λ‹€.
반면 μ°Έμ‘°λ₯Ό μ „λ‹¬ν•˜λŠ” κ²½μš°μ—λŠ” μ›μ‹œκ°’μ—μ„œ ν–ˆλ˜ 것과 같은 κ²°κ³Όλ₯Ό κΈ°λŒ€ν•  수 μžˆλ‹€.
객체의 hello와 hi λ³€μˆ˜λŠ” λ³€μˆ˜λͺ…μ˜ μ£Όμ†Œκ°€ μ„œλ‘œ λ‹€λ₯΄μ§€λ§Œ
value의 μ£Όμ†ŒλŠ” λ™μΌν•˜λ‹€.

λ°˜λ©΄μ— μ•„λž˜ 예제 μ½”λ“œμ—μ„œλŠ”
{greet : 'hello world'} value 값을 hello.greet = 'something'κ³Ό 같이 변경을 해도
hi와 hello 동등 비ꡐλ₯Ό ν–ˆμ„ λ•Œ μ–Έμ œλ‚˜ trueλ₯Ό λ°˜ν™˜ν•œλ‹€.

<script>
var hello = {greet : 'hello world',};

var hi = hello

console.log(hi === hello);//true

hello.greet = 'something';

console.log(hi === hello);//true
</script>

λ‹€μŒ 글은 μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 또 λ‹€λ₯Έ 비ꡐ 곡식 Object.is λŒ€ν•΄ μ•Œμ•„λ³΄μž.

profile
β˜„οΈ

0개의 λŒ“κΈ€