๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

JavaScript

[๋‚ด๋ณด๋‚ด๋ฒˆ] JavaScript - Javascript Coercion Explained

GitHub์— ์žˆ๋Š” 33 Concepts Every JavaScript Developer Should Know ๋ผ๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์˜ ๋‚ด์šฉ ์ค‘ ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์€ ์•„ํ‹ฐํด์„ ๋‚ด๊ฐ€ ์ •๋ฆฌํ•  ๊ฒธ ๊ธฐ์–ตํ•  ๊ฒธ ์ž‘์„ฑํ•˜๋Š” ๋ฒˆ์—ญ๊ธ€์ด๋‹ค. 33๊ฐ€์ง€ ์ค‘ ๋„ค ๋ฒˆ์งธ์ธ Implicit, Explicit, Nominal, Structuring and Duck Typing๊ณผ ๊ด€๋ จ๋œ ๋‚ด์šฉ์ด๋‹ค.

โ€ป ๋‚ด๋ณด๋‚ด๋ฒˆ(๋‚ด๊ฐ€ ๋ณด๋ ค๊ณ  ๋‚ด๊ฐ€ ๋ฒˆ์—ญํ•œ): ์˜์–ด ์ „๊ณต์ž๋„ ํ•ด์™ธ ์œ ํ•™ํŒŒ๋„ ์•„๋‹ˆ๊ธฐ์— ๋ฒˆ์—ญ์—๋Š” ์˜์—ญ, ์˜ค์—ญ, ๊ตฌ๊ธ€ ๋ฒˆ์—ญ์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์˜คํƒ€๋„ ๋งŽ์„ ์ˆ˜ ์žˆ๋‹ค. ์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ์›๋ฌธ์„ ์ง์ ‘ ์‚ดํŽด๋ณด๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ •๋ณด๋“ค์„ ๋” ์ฐพ์•„๋ณด๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•œ๋‹ค.
(ํ•˜์ง€๋งŒ ๋Œ“๊ธ€ ํ”ผ๋“œ๋ฐฑ๋„ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜ƒ )

์ด๋ฒˆ์— ๊ณ ๋ฅธ ์•„ํ‹ฐํด์€ Ben Garrison ๋‹˜์ด ์ž‘์„ฑํ•˜์‹  Javascript Coercion Explained ์ด๋‹ค. ๋‚ด์šฉ ์ค‘๊ฐ„์ค‘๊ฐ„ ๐Ÿค”์ด ์•„์ด์ฝ˜์€ ๋ณธ๋ฌธ์— ๋‚˜์™€ ์žˆ์ง€ ์•Š์€ ๋ฒˆ์—ญ ํ•˜๋ฉฐ ์ถ”๊ฐ€ํ•œ ๋ง์ด๋‹ค. 

 

Javascript Coercion Explained | Hacker Noon

Javascript Coercion Explained Along with some practical examples Before we begin our JS Coercion exploration it’s worthy to note that this post exists because JS is an untyped language(maybe weakly-typed based upon your definition). Typed Language A stro

hackernoon.com


 

๋ช‡ ๊ฐ€์ง€ ์‹ค์šฉ์ ์ธ ์˜ˆ์‹œ์™€ ํ•จ๊ป˜

JS์˜ ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜์— ๋Œ€ํ•ด์„œ ํƒ์‚ฌ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ์— ์•ž์„œ ์ด ํฌ์ŠคํŠธ๋Š” JS๊ฐ€ untyped language(๊ฐœ์ธ์˜ ์ •์˜์— ๋”ฐ๋ผ ์•„๋งˆ weakly-typed ์ผ์ˆ˜๋„)์ด๊ธฐ ๋•Œ๋ฌธ์— ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. (๐Ÿค”: untyped์— ๋Œ€ํ•œ ํ•œ๊ตญ์–ด ํ‘œํ˜„์„ ์ฐพ๋‹ค๊ฐ€ untyped๋ผ๋Š” ๋ง์˜ ์ •์˜์—๋„ ์—ฌ๋Ÿฌ ํ•ด์„์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ์ด ๊ธ€์„ ์ฐธ์กฐํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.) 

Typed Language

strongly-typed programming language๋Š” ๊ฐ ๋ฐ์ดํ„ฐ ํƒ€์ž…(์ •์ˆ˜ํ˜•, ๋ฌธ์žํ˜•, 16์ง„์ˆ˜, ๋ฌถ์Œ ์‹ญ์ง„๋ฒ• ๋“ฑ)์ด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์˜ ์ผ๋ถ€๋ถ„์œผ๋กœ์„œ ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด ์žˆ๊ณ  ์ฃผ์–ด์ง„ ํ”„๋กœ๊ทธ๋žจ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ •์˜๋œ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋“ค์€ ๋ฐ˜๋“œ์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜๋กœ ์„ค๋ช…๋˜์–ด์•ผ ํ•œ๋‹ค. 

-source yo

JS ์‚ฌ์šฉ์ž๋กœ์„œ ์ด๊ฒƒ์ด ์šฐ๋ฆฌ์—๊ฒŒ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์€ ์–ธ์–ด๊ฐ€ ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ํŠน์ • ๊ทœ์น™ ๊ธฐ๋ฐ˜์˜ ํ˜• ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๋„๋ก ๊ฐ•์ œํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ์ด ํฌ์ŠคํŠธ์—์„œ๋Š” 2๊ฐ€์ง€ ๋‹ค๋ฅธ ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜, ๋ช…์‹œ์ (Explicit)๊ณผ ์•”์‹œ์ (Implicit)์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋‹ค. 

๋ช…์‹œ์  ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜์€ ์ฝ”๋“œ ์ž‘์„ฑ์ž์— ์˜ํ•ด ๊ฐ’์„ ํ•œ ํƒ€์ž…์—์„œ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๋ ค๋Š” ๋ช…ํ™•ํ•œ ์‹œ๋„์ด๋‹ค. ์•”์‹œ์  ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜์€ ๋‹ค๋ฅธ ์ž‘์—…์— ๋Œ€ํ•œ ๋œ ๋ถ„๋ช…ํ•œ(less-obvious) ๋ถ€์ž‘์šฉ์œผ๋กœ์„œ ๋ฐœ์ƒํ•œ๋‹ค. ์•„๋ž˜์˜ ์˜ˆ์ œ๋ฅผ ๋ณด๋ผ(gist): 

implicit/explicit coercion

์œ„์˜ ์˜ˆ์ œ์—์„œ ๋‘ ๊ฐ€์ง€ ์ ‘๊ทผ, ์•”์‹œ์ ๊ณผ ๋ช…์‹œ์  ๋ชจ๋‘ ๊ฐ™์€ ๊ฒฐ๊ณผ์ž„์— ์œ ์˜ํ•˜๋ผ. ์ด๊ฒƒ์€ ํฅ๋ฏธ๋กœ์šฐ๋ฉด์„œ๋„ ๋…ผ์Ÿ๋“ค์„ ๋ถˆ๋Ÿฌ์ผ์œผํ‚จ๋‹ค. ์ฆ‰ ๊ฐ€๋…์„ฑ์ด ๋…ผ์Ÿ์˜ ํฌ์ธํŠธ์ด๋‹ค. ๊ฑฐ์˜ ํ•ญ์ƒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋ณด๋‹ค๋Š” ๋‹น์‹ ์ด ์ž์‹ ์˜ ์ฝ”๋“œ๋ฅผ ๋ณผ ๊ฒƒ์ด๋‹ค. ์œ„์˜ ๋ฐฉ๋ฒ• ์ค‘ ์–ด๋Š ๊ฒƒ์ด ๋” ์ฝ๊ธฐ ์‰ฌ์šด๊ฐ€? 

์ด์ œ JS๊ฐ€ ==์™€ ===๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ฐ„ ๋น„๊ต๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€, ์–ธ์ œ null๊ณผ undefined๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž. 

null vs. undefined

์—ฌ๊ธฐ ๋ช‡ ๊ฐ€์ง€ ์ฝ”๋“œ ์˜ˆ์ œ๋“ค์ด ์žˆ๋‹ค. ์•„๋ž˜์— ๋” ์ž์„ธํ•œ ์„ค๋ช…์ด ๋˜์–ด ์žˆ๋‹ค(gist):

null vs undefined types

์œ„์˜ ์ฝ”๋“œ์—์„œ ์ฃผ์š”์ ์€:

  • null์€ ๊ฐ์ฒด์ด๋‹ค
  • undefined๋Š” ๊ณ ์œ ํ•œ ํƒ€์ž…์ด๋‹ค.

๋น„๊ต

์ด์ œ ๋‹ค๋ฅธ ์œ ํ˜•์˜ ๊ฐ’์„ ๋™๋“ฑ ์—ฐ์‚ฐ์ž ==์™€ ์ผ์น˜ ์—ฐ์‚ฐ์ž ===๋กœ ๋น„๊ตํ•œ ๊ฒฐ๊ณผ์— ๋Œ€ํ•ด ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค. 

JS ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์šฐ๋ฆฌ์˜ ์˜๋„์— ๋Œ€ํ•ด ๋ช‡ ๊ฐ€์ง€ ์ถ”์ธกํ•˜๋„๋ก ํ•ด๋ณด์ž. 

undefined/null

undefined์™€ null์— ๋Œ€ํ•ด ๋™๋“ฑ ์—ฐ์‚ฐ์ž(4,5ํ–‰)๋กœ๋Š” ์ผ์น˜ํ•˜๊ณ  ์ผ์น˜ ์—ฐ์‚ฐ์ž(8,9ํ–‰)๋กœ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒƒ์„ ์œ ์˜ํ–ˆ๋‚˜?

String, Number, Boolean์— ๋Œ€ํ•ด์„œ๋„ ๋น„๊ตํ•ด๋ณด์ž: 

string/number/bool

์ง€๊ธˆ ์ฏค์ด๋ฉด ํŒจํ„ด์„ ๋ˆˆ์น˜์ฑ˜์„ ๊ฒƒ์ด๋‹ค. ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’๋“ค์„ ๋™๋“ฑ ์—ฐ์‚ฐ์ž๋กœ ๋น„๊ตํ•˜๋ฉด, JS๋Š” ๊ฐ’์„ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜ํ•  ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์ •ํ™•์„ฑ์€ ์ผ์น˜ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๋‹ฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

๊ฒฐ๋ก 

๊ฒฐ๊ตญ untyped ์–ธ์–ด์—์„œ ๊ฐ•์ œ ํ˜• ๋ณ€ํ™˜์€ ํ•„์š”์•…์ด๋‹ค.

๋‚˜์˜ ์ œ์•ˆ์€:

์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ผ์น˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์•ผ ํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์ •๋‹นํ™”ํ•  ์ˆ˜ ์—†๋‹ค๋ฉด ํ•ญ์ƒ ์ผ์น˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ผ.