๋ฐ์ํ
๐ 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)
- ๊ฐ์ฒด๋ฅผ ๋ฃ์ผ๋ฉด ์ฝ๊ฒ ํ ์ ์๋ค. ( ์ฌ์ฉ์ ์ ์ฅ์์ ๋งค์ฐ ํธ๋ฆฌํจ)
- ๋ค์ด์ค๋ ์ธ์๋ ํ๋์ง๋ง ๋ด๋ถ์์๋ ๋น๊ตฌ์กฐํ๋ฅผ ํตํด ๋๋ ์ง๋ค.
๋ฐ์ํ
'Programming language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
this (0) | 2022.05.24 |
---|---|
ํธ์ด์คํ (Hoisting) (0) | 2020.10.24 |
async vs defer (์ธ๋ถ ์คํฌ๋ฆฝํธ ํ์ผ ๋ถ๋ฌ์ค๊ธฐ) (0) | 2020.10.23 |
[vue] axios ์ ์ญ์ผ๋ก ์ฌ์ฉํ๊ธฐ (0) | 2020.09.14 |