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)
- κ°μ²΄λ₯Ό λ£μΌλ©΄ μ½κ² ν μ μλ€. ( μ¬μ©μ μ μ₯μμ λ§€μ° νΈλ¦¬ν¨)
- λ€μ΄μ€λ μΈμλ νλμ§λ§ λ΄λΆμμλ λΉκ΅¬μ‘°νλ₯Ό ν΅ν΄ λλ μ§λ€.
λ°μν