const flower = {
name: 'Rose',
color: 'Red'
};
const name = flower.name;
const color = flower.color;
console.log(name); // 'Rose'
console.log(color); // 'Red'
flower.name ๊ฐ์ name ๋ณ์์ ๋ฃ๊ณ , flower.color ๊ฐ์ color ๋ณ์์ ๋ฃ์ต๋๋ค.
ํ์ง๋ง ์์ ๊ฐ์ด ๊ฐ์ ๋์
ํ๋ฉด ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ด ๋ฐ์ํฉ๋๋ค. ๋ฐ๋ก ‘name’๊ณผ ‘color’์
๋๋ค.
๋์
๊ณผ์ ์์ name์ด ๋ณ์ ์ ์ธ์ ํ ๋ฒ, ๊ทธ๋ฆฌ๊ณ flower ๊ฐ์ฒด์ ์์ฑ ์ ๊ทผ์ ํ ๋ฒ, ์ด ๋ ๋ฒ์ด ์ฌ์ฉ๋ฉ๋๋ค.
color๋ ๋ง์ฐฌ๊ฐ์ง์
๋๋ค.
๋น๊ตฌ์กฐํ ํ ๋น์ ์ด๋ฐ ๋ฒ๊ฑฐ๋ก์์ ์์ ๊ณ ์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.
๋น๊ตฌ์กฐํ ํ ๋น์ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด์์ ์์ฑ์ ๊ฐ์ ธ์ ๊ฐ์ ์ด๋ฆ์ ๋ณ์์ ๋์
ํ ๋ ์์ฑ๋ช
์ ํ๋ฒ ๋ ๋ช
์ํ ํ์๊ฐ ์์ต๋๋ค.
๊ฒ๋ค๊ฐ ์์ฑ์ด ์ฌ๋ฌ ๊ฐ์ผ์ง๋ผ๋ ํ๋ฒ์ ์ ์ธ์ผ๋ก ํด๊ฒฐ์ด ๊ฐ๋ฅํฉ๋๋ค.
์๋ ์ฝ๋์์๋ ์ ์ฝ๋๋ฅผ ๋ฆฌํฉํ ๋งํ์ฌ ๋น๊ตฌ์กฐํ ํ ๋น์ ์ฌ์ฉํฉ๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { name, color } = flower;
console.log(name); // 'Rose'
console.log(color); // 'Red'
‘const { name, color } = flower’๊ฐ ๋ฐ๋ก ๊ฐ์ฒด ๋น๊ตฌ์กฐํ์
๋๋ค.
์ด ๊ตฌ๋ฌธ์ผ๋ก name๊ณผ color ๋ณ์๋ฅผ ์ ์ธํ๊ณ , flower.name๊ณผ flower.color ์์ฑ์ ๊ฐ์ ๊ฐ์ ์ด๋ฆ ๋ณ์์ ํ ๋นํฉ๋๋ค.
๋ค์์ ๊ฐ์ฒด์ ์์ฑ๊ฐ์ ์ ๊ทผํ๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์
๋๋ค.
const name = flower.name;
const color = flower.color;
// ์์ ์๋๋ ๊ฐ์ ๊ฒฐ๊ณผ
const { name, color } = flower;
์์ฑ๋ช
๊ณผ ๋ณ์๋ช
์ด ์ค๋ณต๋์ง ์์ผ๋ฏ๋ก ๋น๊ตฌ์กฐํ๊ฐ ํจ์ฌ ๊น๋ํ๊ณ ํธ๋ฆฌํ ๊ฒ์ ์ ์ ์์ต๋๋ค.
–
2. ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
๊ฐ์ฒด ๋น๊ตฌ์กฐํ์ ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ๊ฐ๋จํฉ๋๋ค.
const { identifier } = expression;
์ฌ๊ธฐ์ identifier๋ ์์ฑ์ ์ ๊ทผํ ์ด๋ฆ์ด๊ณ , expression์ identifier๊ฐ ์ ๊ทผํ๋ ๊ฐ์ฒด์
๋๋ค.
๋น๊ตฌ์กฐํ๋ฅผ ํ๋ฉด identifier ๋ณ์๋ ์ ๊ทผํ ์์ฑ๊ฐ์ ๊ฐ๊ฒ ๋ฉ๋๋ค.
์์ ๊ฐ์ ์ฝ๋์ด์ง๋ง ์์ฑ ์ ๊ทผ์๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const identifier = expression.identifier;
์ ๋ด์ฉ์ ํ ๋๋ก ๋น๊ตฌ์กฐํ ํ ๋น์ ์ฐ์ตํด ๋ณด๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { name } = flower;
console.log(name); // 'Rose'
‘const { name } = flower;’๋ name ๋ณ์๋ฅผ ์ ์ธํ๊ณ , flower.name์ ์์ฑ๊ฐ์ ๋์
ํ๋ ๋น๊ตฌ์กฐํ ํ ๋น์
๋๋ค.
–
3. ์ฌ๋ฌ ์์ฑ๊ฐ ํ๋ฒ์ ๊ฐ์ ธ์ค๊ธฐ
์ฌ๋ฌ ์์ฑ๊ฐ์ ๊ฐ์ง ๊ฐ์ฒด์ ๋น๊ตฌ์กฐํ๋ ์ผํ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
const { identifier1, identifier2, ..., identifierN } = expression;
์ฌ๊ธฐ์ identifier1, identifier2, …, identifierN์ ์์ฑ์ ์ ๊ทผํ ์ด๋ฆ์ด๊ณ , expression์ identifier๊ฐ ์ ๊ทผํ๋ ๊ฐ์ฒด์
๋๋ค. ๋น๊ตฌ์กฐํ๋ฅผ ํ๋ฉด ๊ฐ identifier ๋ณ์๋ ์ ๊ทผํ ์์ฑ๊ฐ์ ๊ฐ๊ฒ ๋ฉ๋๋ค.
์์ฑ ์ ๊ทผ์๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const identifier1 = expression.identifier1;
const identifier2 = expression.identifier2;
// ......
const identifierN = expression.identifierN;
๊ทธ๋ผ ๋ ๊ฐ์ ์์ฑ๊ฐ์ ํ ๋นํ๋ ์ฒซ ๋ฒ์งธ ์ํ ์ฝ๋๋ฅผ ๋ค์ ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { name, color } = flower;
console.log(name); // 'Rose'
console.log(color); // 'Red'
‘const { name, color } = flower;’๋ name, color ๋ ๋ณ์๋ฅผ ์์ฑํ๊ณ , ๊ฐ๊ฐ flower.name๊ณผ flower.color์ ์์ฑ๊ฐ์ ํ ๋นํฉ๋๋ค.
–
4. ๊ธฐ๋ณธ๊ฐ ์ค์ ํ๊ธฐ
๋น๊ตฌ์กฐํ๋ฅผ ํ๋๋ผ๋ ๊ฐ์ฒด์ ๋์ผํ ์์ฑ๋ช
์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ undefined๊ฐ ํ ๋น๋ฉ๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { meaning } = flower;
console.log(meaning); // undefined
์์ ๊ฐ์ด ์์ฑ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ค์ ์ด ๊ฐ๋ฅํ๋ฉฐ, ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const { identifier = defaultValue } = expression;
identifier๊ฐ expression ๊ฐ์ฒด์ ์์ฑ์ ์ ๊ทผํ๋ ์ด๋ฆ์ด ๋ฉ๋๋ค. expression ๊ฐ์ฒด ๋ด identifier ์์ฑ์ด ์กด์ฌํ์ง ์์ผ๋ฉด ์ง์ ํ ๊ธฐ๋ณธ๊ฐ์ธ defaultValue๊ฐ ๋์
๋ฉ๋๋ค.
๋ค์ ์ฝ๋์ ๊ฐ์ ๊ธฐ๋ฅ์ ํฉ๋๋ค.
const identifier = expression.identifier === undefined ? defaultValue : expression.identifier;
๊ธฐ๋ณธ๊ฐ ์ค์ ์ต์
์ ๋ํด ์ฝ๋๋ฅผ ์์ ํด ๋ณด๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
}
const { meaning = 'love' } = flower;
console.log(meaning) // 'love'
undefined ๋์ ‘love’๊ฐ ๋์
๋ฉ๋๋ค.
–
5. ๋ณ๋ช
(alias) ์ ํ๊ธฐ
๊ฐ์ฒด์ ์์ฑ๋ช
๊ณผ ๋ค๋ฅธ ์ด๋ฆ์ผ๋ก ๋ณ์๋ฅผ ์ ์ธํ๊ณ ์ถ์ ๋๋ ๋ณ๋ช
์ ์ง์ ํ๋ฉด ๋ฉ๋๋ค.
const { identifier: aliasIdentifier } = expression;
์ฌ๊ธฐ์ identifier๋ ์์ฑ์ ์ ๊ทผํ๋ ์ด๋ฆ์ด๋ฉฐ, aliasIdentifier๋ ๋ณ์๋ก ์ฌ์ฉํ ์ด๋ฆ์
๋๋ค. ๋น๊ตฌ์กฐํ๋ฅผ ํ๋ฉด aliasIdentifier๊ฐ ์์ฑ๊ฐ์ ๊ฐ์ต๋๋ค.
๋ค์ ์ฝ๋์ ๊ฐ์ ์๋ฏธ์
๋๋ค.
const aliasIdentifier = expression.identifier;
์ค์ ๋ณ๋ช
์ ์ ์ฉํ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { color: realColor } = flower;
console.log(realColor); // 'Red'
console.log(color); // ์๋ฌ ๋ฐ์(color is not defined)
์ ์ฝ๋๋ realColor๋ก ๋ณ์๋ฅผ ์ ์ธํ๊ณ , flower.color๊ฐ์ ํ ๋นํ์ฌ ๋น๊ตฌ์กฐํ๋ฅผ ์งํํฉ๋๋ค.
–
6. ๋ด๋ถ ๊ฐ์ฒด์ ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
์์์ ํ์ธํ ์ํ์ ์์ํ ๋ฐ์ดํฐ ํ์
(String, Number ๋ฑ)์ ์์ฑ์ ๊ฐ๋ ์ผ๋ฐ ๊ฐ์ฒด์
๋๋ค.
๊ทธ๋ฌ๋ ์ผ๋ถ๋ ๊ฐ์ฒด ์์ ๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ์ต๋๋ค. ๋ฌผ๋ก ์ด๋ฌํ ํํ๋ฅผ ๊ฐ์ง ๊ฐ์ฒด๋ ๋น๊ตฌ์กฐํ ํ ๋น์ด ๊ฐ๋ฅํฉ๋๋ค.
const { nestedObject: { identifier } } = expression;
nestedObject๋ ๋ด๋ถ ๊ฐ์ฒด๋ช
์ด๊ณ , identifier๋ ๋ด๋ถ ๊ฐ์ฒด์ ์์ฑ๋ช
์
๋๋ค.
๋น๊ตฌ์กฐํ ํ ๋น์ ํตํด identifier๊ฐ ๋ด๋ถ ๊ฐ์ฒด์ ์์ฑ๊ฐ์ ๊ฐ์ง๋ฉฐ, ๋ค์ ์ฝ๋์ ๊ฐ์ ์๋ฏธ์
๋๋ค.
const identifier = expression.nestedObject.identifier;
๋ด๋ถ ๊ฐ์ฒด์ ๊น์ด๋ ์ ํ์ด ์์ผ๋ฉฐ, ์ค๊ดํธ{ }๋ง ์ถ๊ฐํด์ฃผ๋ฉด ๋ฉ๋๋ค.
const { propA: { propB: { propC: {......} } } } = object;
๋ค์ ์ํ์ ํตํด ๋ด๋ถ ๊ฐ์ฒด์ ์ ๊ทผํ์ฌ ๋น๊ตฌ์กฐํ ํ ๋น์ ์งํํ๋ ์ฝ๋๋ฅผ ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red',
blooming: {
season: 'spring'
}
};
//Object destructuring
const { blooming: { season } } = flower;
console.log(season) // 'spring'
‘const { blooming: { season } } = flower;’๋ ๋ด๋ถ ๊ฐ์ฒด blooming์ season ์์ฑ๊ฐ์ ํ ๋นํฉ๋๋ค.
–
7. ๋์ ์ด๋ฆ์ผ๋ก ์์ฑ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
๋์ ์ด๋ฆ(Dynamic Name)์ ์ฌ์ฉ๋ ๊ฐ๋ฅํฉ๋๋ค.
const { [propName]: identifier } = expression;
propName์ ์์ฑ๋ช
(์ฃผ๋ก string ํ์
)์ด๋ฉฐ, identifier๋ ๊ฐ์ด ํ ๋น๋ ๋ณ์์ ์ด๋ฆ์
๋๋ค.
๋ค์ ์ฝ๋์ ๊ฐ์ ์๋ฏธ๋ฅผ ๊ฐ์ต๋๋ค.
const identifier = expression[propName];
๋ค์ ์ํ์ ํตํด ์์ฑ๋ช
์ผ๋ก ์ฌ์ฉ๋๋ prop์ ํ์ธํ๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const prop = 'name';
const { [prop]: flowerName } = expression;
console.log(flowerName); // 'Rose'
‘const { [prop]: flowerName } = expression’์์ flowerName ๋ณ์์ flower[prop] ์์ฑ๊ฐ์ ํ ๋นํ๋ฉฐ, prop์ ์ ๊ทผํ ์์ฑ๋ช
์ ๊ฐ์ง ๋ณ์์
๋๋ค.
–
8. ๋น๊ตฌ์กฐํ ํ ๋น ํ ๋จ๊ฒจ์ง ๊ฐ์ฒด
๋๋จธ์ง ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ ๋ฌธ๋ฒ์ ๋น๊ตฌ์กฐํ ํ ๋น์์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค.
const { identifier, ...rest } = expression;
์ ์ฝ๋์์ identifier๋ฅผ ์ ์ธํ ๋๋จธ์ง ์์ฑ์ rest ๋ณ์์ ํ ๋น๋ฉ๋๋ค.
์๋ ์ฝ๋์์ name ์์ฑ์ ์ ์ธํ ๋๋จธ์ง ์์ฑ์ ์ ๋ฆฌํ๋ ์ํ์ ํ์ธํด ๋ณด๊ฒ ์ต๋๋ค.
const flower = {
name: 'Rose',
color: 'Red'
};
const { name, ...realColor } = flower;
console.log(realColor) // { realColor: 'Red' }
name ์์ฑ๊ฐ์ ํ ๋นํ ๋ค ๋๋จธ์ง ์์ฑ๋ค์ realColor ๋ณ์์ ํ ๋นํฉ๋๋ค.
–
9. ์์ฃผ ์ฌ์ฉ๋๋ ๊ตฌ๋ฌธ
์์์ ํ์ธํ๋๋ก ๋น๊ตฌ์กฐํ๋ ์์ฑ๊ฐ์ ๋ณ์์ ํ ๋นํฉ๋๋ค.
๋ณ์ ํ์
์ const, let, var ๋ชจ๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ฉฐ, ์ด๋ฏธ ์ ์ธ๋ ๋ณ์์๋ ํ ๋น์ด ๊ฐ๋ฅํฉ๋๋ค.
let์ ์ฌ์ฉํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
//let
const flower = {
name: 'Rose',
};
let { name } = flower;
console.log(name); // 'Rose'
var๋ฅผ ์ฌ์ฉํ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
//var
const flower = {
name: 'Rose',
};
var { name } = flower;
console.log(name); // 'Rose'
์ด๋ฏธ ์ ์ธ๋ ๋ณ์์ ํ ๋นํ๋ ๋ฐฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
//existing variable
let name;
const flower = {
name: 'Rose',
};
({ name } = flower);
console.log(name); // 'Rose'
for…of ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ ๋น๊ตฌ์กฐํ ํ ๋น์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const flowers = [
{ name: 'Rose' },
{ name: 'Lily' }
];
for (const { name } of flowers) {
console.log(name) //'Rose', 'Lily'
}
์ฌ์ค ๋น๊ตฌ์กฐํ๋ ํ ๋น์ด ๊ฐ๋ฅํ ๊ณณ์ด๋ฉด ์ด๋๋ ์ง ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค๊ณ ๋ณผ ์ ์์ต๋๋ค.
์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํจ์ ํ๋ผ๋ฏธํฐ์ ๋น๊ตฌ์กฐํ๋ ๊ฐ๋ฅํฉ๋๋ค.
const flowers = [
{ name: 'Rose' },
{ name: 'Lily' }
];
const names = flowers.map(
function({ name }) {
return name;
}
)
console.log(names); // ['Rose', 'Lily']
‘function({ name })’์ ํจ์์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋น๊ตฌ์กฐํํ์ฌ, name ๋ณ์์ name ์์ฑ์ ํ ๋นํฉ๋๋ค.
10. ๋ณ์ ๊ฐ ์๋ก ๊ตํ(swap)
๋น๊ตฌ์กฐํ ํ ๋น์ ์ฌ์ฉํ๋ฉด ๋ ๋ณ์์ ๊ฐ์ ํธ๋ฆฌํ๊ฒ ๊ตํํ ์๋ ์์ผ๋ฉฐ, ๊ธฐ์กด์ฒ๋ผ ๋ฐ์ดํฐ๋ฅผ ์์๋ก ๋ณด๊ดํ ๋ณ์๊ฐ ํ์ํ์ง ์์ต๋๋ค.
let a = 1;
let b = 2;
//๊ธฐ์กด ๊ตํ ๋ฐฉ๋ฒ
const temp = a;
a = b;
b = temp;
//๋น๊ตฌ์กฐํ๋ฅผ ํตํ ๊ตํ
[a, b] = [b, a];