# ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•(Destructuring)

๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์ด๋ผ๊ณ  ํ•˜๋Š” ์ด ES6 ๋ฌธ๋ฒ•์€ ํ•œ๊ธ€๋กœ ๋ฒˆ์—ญํ•˜๋ฉด ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ๊ตฌ์กฐ ๋ถ„ํ•ด(?)๋ผ๋Š” ์–ต์ง€์Šค๋Ÿฌ์šด ์šฉ์–ด๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์ „์— ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” '๊ตฌ์กฐ'๋ผ๋Š” ๋‹จ์–ด๋ฅผ ๋จผ์ € ํŒŒ์•…ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# ๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ '๊ตฌ์กฐ'

๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

var arr = [1, 2, 3, 4];
var obj = {
  a: 10,
  b: 20,
  c: 30
};

์œ„ ์ฝ”๋“œ๋Š” ์ด์ƒํ•  ๊ฒƒ ์—†๋Š” ์ „ํ˜•์ ์ธ ๊ฐ์ฒด, ๋ฐฐ์—ด ์„ ์–ธ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์™ผ์ชฝ์— ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ๋„ฃ๊ณ  ์˜ค๋ฅธ์ชฝ์— ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์„ ์–ธํ•˜์ฃ . '๊ตฌ์กฐ'๋ผ๋Š” ๋‹จ์–ด๋Š” ์ด๋Ÿฌํ•œ ์„ ์–ธ ํ˜•์‹์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ '๊ตฌ์กฐ ๋ถ„ํ•ด'๋ž€ ๋ฌด์—‡์ผ๊นŒ์š”? ๋ฐ”๋กœ ์ด๋Ÿฌํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ ํ˜•์‹์ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž์œ ๋กœ์›Œ์ง€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

var { a, b, c } = obj;

๋ณ€์ˆ˜์— ์ค‘๊ด„ํ˜ธ๋ฅผ ์”Œ์šฐ๋Š” ๊ฒƒ ์ž์ฒด๊ฐ€ ์–ด์ƒ‰ํ•˜๊ณ  ๋‚ฏ์„ค๊ธฐ๋งŒ ํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ผ๋ฉด ์˜ค๋ฅ˜๋ฅผ ๋‚ด๋ฟœ์—ˆ์„ ํ…๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ˜์†”์„ ์ฐ์–ด๋ณด๋ฉด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

console.log(a); // 10
console.log(b); // 20
console.log(c); // 30

# ํŠน์ • ๊ฐ์ฒด์˜ ๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š” ๋ฐฉ๋ฒ•

์‹ ๊ธฐํ•˜๋„ค์š”. ์ž ๊ทธ๋Ÿผ ์ด๊ฑด ์–ด๋””๋‹ค๊ฐ€ ์จ๋จน์–ด์•ผ ๋˜๋Š” ๊ฑธ๊นŒ์š”? ๊ธฐ์กด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํŠน์ • ๊ฐ์ฒด์˜ ๊ฐ’์„ ๊บผ๋‚ด์˜ฌ ๋•Œ ๋ณดํ†ต ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

var josh = {
  language: 'javascript',
  position: 'front-end',
  area: 'pangyo',
  hobby: 'singing',
  age: '102'
};

var language = josh.language;
var position = josh.position;
var area = josh.area;
var hobby = josh.hobby;
var age = josh.age;

์—ฌ๊ธฐ์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ ์€ ๊ฐ์ฒด์˜ ํŠน์ • ์†์„ฑ ๊ฐ’์„ ๊บผ๋‚ด์˜ฌ ๋•Œ๋งˆ๋‹ค ์ผ์ผ์ด ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ๋‹ด์•„์ค˜์•ผ ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๊บผ๋‚ด์•ผ ํ•  ์†์„ฑ์ด ๋งŽ์œผ๋ฉด ๋งŽ์„์ˆ˜๋ก ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋Œ€์ž…ํ•˜๋Š” ์‹์˜ ๋ฐ˜๋ณต ์ž‘์—…์„ ๊ณ„์†ํ•ด์ค˜์•ผ๊ฒ ์ฃ . ์œ„ ์ฝ”๋“œ๋งŒ ๋ด๋„ 5๊ฐœ ์†์„ฑ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ๊ฐœํ–‰์„ 4๋ฒˆ ํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์ ์šฉํ•˜๋ฉด ํ›จ์”ฌ ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ๊บผ๋‚ด์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var josh = {
  language: 'javascript',
  position: 'front-end',
  area: 'pangyo',
  hobby: 'singing',
  age: '102'
};

var { language, position, area, hobby, age } = josh;
console.log(language); // javascript
console.log(position); // front-end
console.log(area); // pangyo
console.log(hobby); // singing
console.log(age); // 102

์ด๋ ‡๊ฒŒ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ๋ผ์ธ ์ˆซ์ž๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ , ์ด ๋ฌธ๋ฒ•์ด ์ต์ˆ™ํ•ด์ง€๋ฉด ์ „์ฒด์ ์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•ด์ง€๋Š” ๊ฒƒ์„ ๋Š๋‚„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# ๋ทฐ์—‘์Šค์— ์ ์šฉํ•˜๋Š” ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ• 1

๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜๊ธฐ ๊ฐ€์žฅ ์ข‹์€ ๊ณณ์€ ๋ฐ”๋กœ ๋ทฐ์—‘์Šค์˜ actions ์†์„ฑ์ž…๋‹ˆ๋‹ค. ๋ทฐ์—‘์Šค์˜ actions ์†์„ฑ๋“ค์€ ๋ชจ๋‘ context๋ผ๋Š” ์ธ์ž๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  context์˜ commit API๋ฅผ ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

actions: {
  fetchData(context) {
    context.commit('addProducts');
  }
}

์—ฌ๊ธฐ์„œ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์ ์šฉํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

actions: {
  fetchData({ commit }) {
    commit('addProducts');
  }
}

์–ด๋–ป๊ฒŒ ์ด๋ ‡๊ฒŒ ์ ์šฉ๋œ ๊ฑธ๊นŒ์š”? ์œ„ ๋ถ„ํ•ด ๊ณผ์ •์„ ์ข€ ๋” ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ €, ๋ฌธ๋ฒ•์ ์ธ ๋ถ€๋ถ„์„ ๋– ๋‚˜์„œ context๋ผ๋Š” ๊ฐ์ฒด์—๋Š” ์ด๋ฏธ commit ์†์„ฑ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.



ย 



actions: {
  fetchData(context) {
    context.commit('addProducts');
  }
}

๊ทธ๋Ÿผ context ๊ฐ์ฒด๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

var context = {
  commit: actionName => console.log(actionName +' has been committed!!')
};

์—ฌ๊ธฐ์— ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์ ์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

var { commit } = context;
commit('addProducts'); // addProducts has been committed!!

TIP

๋ทฐ์—‘์Šค๋ž€ ๋ทฐ์˜ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Vuex ํŠœํ† ๋ฆฌ์–ผ (opens new window)์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

# ๋ทฐ์— ์ ์šฉํ•˜๋Š” ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ• 1

๋ทฐ์—์„œ data, methods ๋“ฑ์— ์ ‘๊ทผํ•  ๋•Œ this๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋•Œ๋•Œ๋กœ ๋งค๋ฒˆ this๋ฅผ ํƒ€์ดํ•‘ํ•ด ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์€ ๋ฒˆ๊ฑฐ๋กœ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฒˆ๊ฑฐ๋กœ์›€์„ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

<script>
export default {
  props: {
    id: Number,
    title: String,
    created: String,
    author: String,
  },
  methods: {
    savePost(postId) {
      // ..
    },
    submitForm() {
      // ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ• ๋ฏธ์ ์šฉ
      this.savePost(this.id);

      // ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ• ์ ์šฉ
      const { id, savePost } = this;
      savePost(id);
    },
  },
}
</script>

์œ„ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด this๋ฅผ ๋งค๋ฒˆ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜„์žฌ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฐ์ดํ„ฐ์ธ์ง€, ์™ธ๋ถ€์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ์ธ์ง€ ๊ตฌ๋ถ„์ด ์–ด๋ ค์›Œ์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ์ ํŠธ์— ์ •ํ•ด์ง„ ์ปจ๋ฒค์…˜์— ๋งž์ถฐ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

# ๋ทฐ์— ์ ์šฉํ•˜๋Š” ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ• 2

v-for ๋””๋ ‰ํ‹ฐ๋ธŒ (opens new window)๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

posts๋ผ๋Š” ๋ฐฐ์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜๋ณต ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด v-for ๋””๋ ‰ํ‹ฐ๋ธŒ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.
๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด v-for ๋””๋ ‰ํ‹ฐ๋ธŒ ์•ˆ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ถ€๋ถ„์— ๋งค๋ฒˆ ์‹๋ณ„์ž post๋ฅผ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„๋„ ์•ž์„œ ๋‚˜์˜จ ๊ฒƒ์ฒ˜๋Ÿผ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์ ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<template>
  <ul>
    <li v-for="post in posts" :key="post.id">
      {{ post.title }} - {{ post.author }}
    </li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      posts: [
        { id: 1, title: "post 1", created: "2021-08-01", author: "Capt" },
        { id: 2, title: "post 2", created: "2021-08-02", author: "Capt" },
        { id: 3, title: "post 3", created: "2021-08-03", author: "Capt" },
      ],
    };
  },
}
</script>

๋‹ค์Œ ์ฝ”๋“œ๋Š” ์œ„ ์ฝ”๋“œ์— ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์ ์šฉํ•œ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ์ด์ „๋ณด๋‹ค ๊ฐ„๊ฒฐํ•ด์ง„ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ณด๋‹ค ์—ฌ๋Ÿฌ ์†์„ฑ์„ ์ฐธ์กฐํ•ด์•ผ ํ•˜๋Š” ๊ธด ์ฝ”๋“œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋” ๊ฐ„๋‹จํ•˜๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<li v-for="{ title, author, id } in posts" :key="id">
  {{ title }} - {{ author }}
</li>