๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Frontend/React

[ ๋ฆฌ์•กํŠธ(React) ] ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น ์‹œ ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋ณ€๊ฒฝํ•˜๊ธฐ

by YWTechIT 2021. 6. 1.
728x90

๐Ÿ“ ๊ตฌ์กฐ ๋ถ„ํ•ดํ• ๋‹น ์‹œ ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋ณ€๊ฒฝํ•˜๊ธฐ

๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น(Destructuring_assignment)์ด๋ž€ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” JS ํ‘œํ˜„์‹์ด๋‹ค. (์ถœ์ฒ˜: MDN)

 

์˜ˆ๋ฅผ๋“ค์–ด information ๊ฐ์ฒด(object)๊ฐ€ alert ํ•จ์ˆ˜์— ์ธ์ž(parameter)๋กœ ๋„˜์–ด๊ฐ€๋Š”๋ฐ ์ด๋•Œ ๋น„๊ตฌ์กฐํ• ๋‹น์œผ๋กœ ์ธ์ž๋ฅผ ํ‘œํ˜„ํ•ด๋ณด์ž. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œํ˜„ ๋  ์ˆ˜ ์žˆ๋‹ค.

// parameter: Object
information = {
  name: 'AYW',
  age: 27,
  address: 'DongTan',
}

// alert Function
const alert = ({ name, age, address }) => {
  return {name, age, address}
}

// declare alert(information)
const myInformation = alert(information)
๐Ÿ‘‰๐Ÿฝ { name: 'AYW', age: 27, address: 'DongTan'}

 

์—ฌ๊ธฐ์„œ ๋น„๊ตฌ์กฐ ํ• ๋‹น์œผ๋กœ ๋„˜์–ด์˜จ ๊ธฐ์กด ๋ณ€์ˆ˜๋ฅผ ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ํ• ๋‹นํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด name์„ nickName, age๋ฅผ number, address๋ฅผ whereLive๋กœ ๋ณ€๊ฒฝํ•ด๋ณด์ž.

// change parameter name at alert Function
const alert = ( information ) => {
  const { name: nickName, age: number, address: whereLive } = information;
  return { nickName, number, whereLive };
};

// declare alert(information)
const myInformation = alert(information)
๐Ÿ‘‰๐Ÿฝ { nickName: 'AYW', number: 27, whereLive: 'DongTan'}

์ด์ฒ˜๋Ÿผ ํ˜„์žฌ ๋ณ€์ˆ˜ ์ด๋ฆ„: ๋ณ€๊ฒฝ ํ•  ์ด๋ฆ„ = parameter ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์†์‰ฝ๊ฒŒ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€