๐ก ๋์ ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ ๊ฐ์ด๋๋ฅผ ์ฝ๊ณ ์ ๋ฆฌํฉ๋๋ค.
6.1 ๊ฐ์ฒด ๋์คํธ๋ญ์ฒ๋ง
// ES6 ์ด์
var person = {
first: "Alberto",
last: "Montalesi"
}
var first = person.first;
var last = person.last;
// ES6
const person = {
first: "Alberto",
last: "Montalesi"
}
const { first, last } = person;
์ค์ฒฉ๋ ๊ฐ์ฒด ํํ๋ก ๋ฐ์ดํฐ๊ฐ ์ฃผ์ด์ง ๊ฒฝ์ฐ์๋ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
const person = {
name: "Alberto",
last: "Montalesi",
links:{
social: {
facebook: "https://www.facebook.com/alberto.montalesi",
},
website: "http://albertomontalesi.github.io/"
}
}
const { facebook } = person.links.social;
๋ค์๊ณผ ๊ฐ์ด ๋ณ์ ์ด๋ฆ์ ๋ฐ๊พธ์ด ์ง์ ํ ์๋ ์๋ค.
const { facebook:fb } = person.links.social;
// person.links.social.facebook ํ๋กํผํฐ๋ฅผ ์ฐพ์ fb๋ผ๋ ๋ณ์๋ก ๋ช
๋ช
console.log(fb); // https://www.facebook.com/alberto.montalesi
console.log(facebook); //ReferenceError: facebook is not defined
๊ธฐ๋ณธ๊ฐ์ ์ ๋ฌํ ์๋ ์๋ค.
const { facebook:fb = "https://www.facebook.com"} = person.links.social;
6.2 ๋ฐฐ์ด ๋์คํธ๋ญ์ฒ๋ง
๊ธฐ๋ณธ ๋ฌธ๋ฒ
const person = ["Alberto","Montalesi",25];
const [name,surname,age] = person;
์์ฑํ๋ ค๋ ๋ณ์๊ฐ ๋ฐฐ์ด์ ์์๋ณด๋ค ์ ์ ๊ฒฝ์ฐ
const person = ["Alberto","Montalesi",25];
const [name,surname] = person;
console.log(name,surname);
// Alberto Montalesi
๋๋จธ์ง ๋ชจ๋ ๊ฐ์ ์ป๊ณ ์ถ์ ๊ฒฝ์ฐ: rest ์ฐ์ฐ์ ์ฌ์ฉ
const person = ["Alberto", "Montalesi", "pizza", "ice cream", "cheese cake"];
const [name,surname,...food] = person ;
console.log(food);
// Array [ "pizza", "ice cream", "cheese cake" ]
6.3 ๋์คํธ๋ญ์ฒ๋ง์ ์ด์ฉํ์ฌ ๋ณ์ ๊ต์ฒดํ๊ธฐ
๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ๋ฉด ๋ณ์์ ๊ฐ์ ๋งค์ฐ ์ฝ๊ฒ ์๋ก ๊ต์ฒดํ ์ ์๋ค.
let hungry = "yes";
let full = "no";
// after we eat we don't feel hungry anymore, we feel full, let's swap the values
[hungry, full] = [full, hungry];
console.log(hungry,full);
// no yes
'Tech > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Chapter 08: ๋ฐฐ์ด ๋ฉ์๋ (Array improvements) (0) | 2022.01.22 |
---|---|
Chapter 07: ๋ฐ๋ณต๋ฌธ (Loop) (0) | 2022.01.20 |
Chapter 05: ๋ฌธ์์ด ๋ฉ์๋ (Additional string methods) (0) | 2022.01.18 |
Chapter 04: ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด (Template literals) (0) | 2022.01.16 |
Chapter 03: ํจ์ ๊ธฐ๋ณธ๊ฐ ์ธ์ (Default function arguments) (0) | 2022.01.13 |