๐Ÿงฑ๐Ÿงฑ๐Ÿงฑ JS์˜ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด: ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •

5135 ๋‹จ์–ด javascripttutorialwebdevbeginners
์›น ๊ฐœ๋ฐœ ๐Ÿš€๐Ÿš€๐Ÿš€์„ ๋” ์ž˜ํ•˜๊ณ  ์‹ถ์œผ์„ธ์š”? https://codesnacks.net/subscribe/์—์„œ ๋‚ด ์ฃผ๊ฐ„ ๋‰ด์Šค๋ ˆํ„ฐ๋ฅผ ๊ตฌ๋…ํ•˜์‹ญ์‹œ์˜ค.


JavaScript์—์„œ ๊ฐ์ฒด๋ฅผ ๊ตฌ์กฐํ™”ํ•  ๋•Œ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค.

๊ตฌ์กฐ ๋ถ„ํ•ด๊ฐ€ ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๋‹ค์‹œ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const pastry = {
  name: "waffle"
};

const { name }ย = pastry;
console.log(name); // waffle

์กด์žฌํ•˜์ง€ ์•Š๋Š” pastry ๊ฐ์ฒด์˜ ์†์„ฑ์— ์•ก์„ธ์Šคํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์–ด๋–ค ์ผ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€ ๋ด…์‹œ๋‹ค.

const pastry = {
  name: "waffle"
};

const { sweetness }ย = pastry;
console.log(sweetness); // undefined

Destructuring ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์†์„ฑ์ด ์ •์˜๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const { sweetness = 70 }ย = pastry;
console.log(sweetness); // 70

๊ธฐ๋ณธ๊ฐ’์€ ์†์„ฑ์ด ์‹ค์ œ๋กœ undefined ์ธ ๊ฒฝ์šฐ์—๋งŒ ์„ค์ •๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ false , null ๋˜๋Š” 0 ์™€ ๊ฐ™์€ ๋‹ค๋ฅธ null ๊ฐ’์— ๋Œ€ํ•œ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ๊ฐ’๊ณผ ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ๋ฅผ ๊ฒฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„ ๋ณ€๊ฒฝ์ด ์–ด๋–ป๊ฒŒ ์ž‘๋™ํ•˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const pastry = {
  name: "waffle",
  tastiness: 100,
};

// let's get the value of tastiness and store it in deliciousness
const { tastiness: deliciousness }ย = pastry;
console.log(deliciousness); // 100

// and now let's combine renaming and defaults

// tastiness is set, so no default will be used
const { tastiness: palatability = 75 }ย = pastry;
console.log(palatability); // 100

// sweetness is undefined; the default will be used
const { sweetnewss: sweet = 50 }ย = pastry;
console.log(sweet); // 50



์›น ๊ฐœ๋ฐœ์„ ๋” ์ž˜ํ•˜๊ณ  ์‹ถ์œผ์‹ญ๋‹ˆ๊นŒ?
๐Ÿš€๐Ÿš€๐Ÿš€subscribe to the Tutorial Tuesday โœ‰๏ธnewsletter

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ