Programming language/JavaScript

[Vue] Vuex ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—...

deo2kim 2020. 7. 10. 13:57
๋ฐ˜์‘ํ˜•

๐Ÿš— ES6+ ์—์„œ Obj๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์„ ์–ธ

const name = 'deok'
const obj1 = {
    name: name,
    sayHello: function() {
        return `Hi my name is ${this.name}`
    }
}

const obj2 = {
    name,
    sayHello() {
        return `Hi my name is ${this.name}`
    }
}
  • obj1๊ณผ ob2๋Š” ์™„์ „ํžˆ ๊ฐ™๋‹ค.

๐Ÿš“ ๊ฐ์ฒด์˜ ๋น„๊ตฌ์กฐํ™”(destructuring)

const student = {
    name: 'deok',
    email: 'deok@deok.com',
    phone: '01012345678'
}

// 1๋ฒˆ
// const name = student.name
// const email = student.email
// const phone = student.phone

// 2๋ฒˆ
// const { name } = student
// const { email } = student
// const { phone } = student

// 3๋ฒˆ
const { name, email, phone } = student
  • ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค๋Š” ๊ฐœ๋…์ด ์•„๋‹Œ student ๊ฐ์ฒด๋ฅผ ํ•ด์ฒดํ•˜๊ฒ ๋‹ค๋Š” ๋Š๋‚Œ.
  • ํ‚ค์™€ ๋ณ€์ˆ˜๋ช…์ด ๊ฐ™์•„์•ผํ•œ๋‹ค. ์ˆœ์„œ๋Š” ์ƒ๊ด€ ์—†์Œ.
function getStudentInfo1(student) {
    console.log(`Hi, my name is ${student.name}, email is ${student.email}`)
}

function getStudentInfo2({ name, email, phone }) {
    console.log(`Hi, my name is ${name}, email is ${email}`)
}
  • {} ๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด์˜จ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์—ฌ ๋จผ์ € ๋ถ„ํ•ด๋ฅผ ํ•˜๊ณ  ์‹œ์ž‘.
function saveStudent1(name, email, phone, id, ...) {
    return 
}
saveStudent('deok', '@deok.com', '01012345678')

function saveStudent2({ name, email, phone }) {
    return
}
saveStudent(student)
  • ๊ฐ์ฒด๋ฅผ ๋„ฃ์œผ๋ฉด ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ( ์‚ฌ์šฉ์ž ์ž…์žฅ์—์„œ ๋งค์šฐ ํŽธ๋ฆฌํ•จ)
  • ๋“ค์–ด์˜ค๋Š” ์ธ์ž๋Š” ํ•˜๋‚˜์ง€๋งŒ ๋‚ด๋ถ€์—์„œ๋Š” ๋น„๊ตฌ์กฐํ™”๋ฅผ ํ†ตํ•ด ๋‚˜๋ˆ ์ง„๋‹ค.
๋ฐ˜์‘ํ˜•