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)
  • 객체λ₯Ό λ„£μœΌλ©΄ μ‰½κ²Œ ν•  수 μžˆλ‹€. ( μ‚¬μš©μž μž…μž₯μ—μ„œ 맀우 νŽΈλ¦¬ν•¨)
  • λ“€μ–΄μ˜€λŠ” μΈμžλŠ” ν•˜λ‚˜μ§€λ§Œ λ‚΄λΆ€μ—μ„œλŠ” 비ꡬ쑰화λ₯Ό 톡해 λ‚˜λˆ μ§„λ‹€.
λ°˜μ‘ν˜•