๋์คํธ๋ญ์ฒ๋ง ํ ๋น = ๊ตฌ์กฐ ๋ถํด ํ ๋น ์ ๊ตฌ์กฐํ๋ ๋ฐฐ์ด๊ณผ ๊ฐ์ ์ดํฐ๋ฌ๋ธ ๋๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตฌ์กฐํํ์ฌ 1๊ฐ ์ด์์ ๋ณ์์ ๊ฐ๋ณ์ ์ผ๋ก ํ ๋นํ๋ ๊ฒ์ด๋ค. ๋ฐฐ์ด๊ณผ ๊ฐ์ ์ดํฐ๋ฌ๋ธ ๋๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด์์ ํ์ํ ๊ฐ๋ง ์ถ์ถํ์ฌ ๋ณ์์ ํ ๋นํ ๋ ์ ์ฉํ๋ค.
๊ตฌ์กฐํ๋ ๋ฐฐ์ด์ ๋์คํธ๋ญ์ฒ๋ง ( ๋น๊ตฌ์กฐํ )ํ์ฌ 1๊ฐ ์ด์์ ๋ณ์์ ํ ๋นํด๋ณด์.
// ES5
var arr = [1, 2, 3];
var one = arr[0];
var two = arr[1];
var three = arr[2];
console.log(one, two, three); // 1 2 3
const arr = [1, 2, 3];
// ES6 ๋ฐฐ์ด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น
// ๋ณ์ one, two, three๋ฅผ ์ ์ธํ๊ณ ๋ฐฐ์ด arr์ ๋์คํธ๋ญ์ฒ๋งํ์ฌ ํ ๋นํ๋ค.
// ์ด๋ ํ ๋น ๊ธฐ์ค์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ค.
const [one, two, three] = arr;
console.log(one, two, three); // 1 2 3
ํ ๋น๋ฌธ์ ์ฐ๋ณ์ ์๋ ํ ๋น์ ๋์์ ์ดํฐ๋ฌ๋ธ์ด์ด์ผํ๋ค. ํ ๋น ๊ธฐ์ค์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ด๋ค. = ์์๋๋ก ํ ๋น๋๋ค.
const [x, y] = [1, 2];
์ฐ์ฐ์ ์ผ์ชฝ์ ๊ฐ์ ํ ๋น๋ฐ์ ๋ณ์๋ฅผ ๋ฐฐ์ด ๋ฆฌํฐ๋ด ํํ๋ก ์ ์ธํ๋ค.
const [x, y]; // SyntaxError: Missing initializer in destructuring declaration
const [a, b] = {}; // TypeError: {} is not iterable
์ฐ๋ณ์ ์ดํฐ๋ฌ๋ธ์ ํ ๋นํ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
let x, y;
[x, y] = [1, 2];
const ํค์๋๋ก ๋ณ์๋ฅผ ์ ์ธํ ์ ์์ด์ ๊ถ์ฅํ์ง ์๋ ๋ฐฉ๋ฒ์ด๋ค.
const [a, b] = [1, 2];
console.log(a, b); // 1 2
const [c, d] = [1];
console.log(c, d); // 1 undefined
const [e, f] = [1, 2, 3];
console.log(e, f); // 1 2
const [g, , h] = [1, 2, 3];
console.log(g, h); // 1 3
๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ๋ฐฐ์ด์ ์์๋๋ก ํ ๋น๋๋ค. ๋ณ์์ ๊ฐ์์ ์ดํฐ๋ฌ๋ธ์ ์์ ๊ฐ์๊ฐ ๋ฐ๋์ ์ผ์นํ์ง ์์๋ ๋๋ค.
// ๊ธฐ๋ณธ๊ฐ
const [a, b, c = 3] = [1, 2];
console.log(a, b, c); // 1 2 3
// ๊ธฐ๋ณธ๊ฐ๋ณด๋ค ํ ๋น๋ ๊ฐ์ด ์ฐ์ ํ๋ค.
const [e, f = 10, g = 3] = [1, 2];
console.log(e, f, g); // 1 2 3
์ข๋ณ์ ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํด์ฃผ์๋ค. c
ํ ๋น๋ ๊ฐ์ด ์ฐ์ ์์์ด๋ค. f
// url์ ํ์ฑํ์ฌ protocol, host, path ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด ๋ฐํํ๋ค.
function parseURL(url = '') {
// '://' ์์ ๋ฌธ์์ด(protocol)๊ณผ '/' ์ด์ ์ '/'์ผ๋ก ์์ํ์ง ์๋ ๋ฌธ์์ด(host)๊ณผ '/' ์ดํ์ ๋ฌธ์์ด(path)์ ๊ฒ์ํ๋ค.
const parsedURL = url.match(/^(\w+):\/\/([^/]+)\/(.*)$/);
console.log(parsedURL);
/*
[
'https://developer.mozilla.org/ko/docs/Web/JavaScript',
'https',
'developer.mozilla.org',
'ko/docs/Web/JavaScript',
index: 0,
input: 'https://developer.mozilla.org/ko/docs/Web/JavaScript',
groups: undefined
]
*/
if (!parsedURL) return {};
// ๋ฐฐ์ด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น์ ์ฌ์ฉํ์ฌ ์ดํฐ๋ฌ๋ธ์์ ํ์ํ ์์๋ง ์ถ์ถํ๋ค.
const [, protocol, host, path] = parsedURL;
return { protocol, host, path };
}
const parsedURL = parseURL('https://developer.mozilla.org/ko/docs/Web/JavaScript');
console.log(parsedURL);
/*
{
protocol: 'https',
host: 'developer.mozilla.org',
path: 'ko/docs/Web/JavaScript'
}
*/
์ดํฐ๋ฌ๋ธ์์ ํ์ํ ์์๋ง ์ถ์ถํ์ฌ ๋ณ์์ ํ ๋นํ๊ณ ์ถ์ ๋ ์ ์ฉํ๋ค.
// Rest ์์
const [x, ...y] = [1, 2, 3];
console.log(x, y); // 1 [ 2, 3 ]
Rest ํ๋ผ๋ฏธํฐ์ ์ ์ฌํ๊ฒ Rest Element ๋ฅผ ์ฌ์ฉํ ์ ์๋ค. Rest ์์๋ Rest ํ๋ผ๋ฏธํฐ์ ๊ฐ๊ฒ ๋ฐ๋์ ๋ง์ง๋ง์ ์์นํด์ผ ํ๋ค.
๊ฐ์ฒด์ ๊ฐ ํ๋กํผํฐ๋ฅผ ๊ฐ์ฒด๋ก๋ถํฐ ๋์คํธ๋ญ์ฒ๋งํ์ฌ ๋ณ์์ ํ ๋นํด๋ณด์.
// ES5
var user = { firstName: 'Ungmo', lastName: 'Lee' };
var firstName = user.firstName;
var lastName = user.lastName;
console.log(firstName, lastName); // Ungmo Lee
const user = { firstName: 'Ungmo', lastName: 'Lee' };
// ES6 ๊ฐ์ฒด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น
// ๋ณ์ lastName, firstName์ ์ ์ธํ๊ณ user ๊ฐ์ฒด๋ฅผ ๋์คํธ๋ญ์ฒ๋งํ์ฌ ํ ๋นํ๋ค.
// ์ด๋ ํ๋กํผํฐ ํค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋์คํธ๋ญ์ฒ๋ง ํ ๋น์ด ์ด๋ฃจ์ด์ง๋ค. ์์๋ ์๋ฏธ๊ฐ ์๋ค.
const { lastName, firstName } = user;
console.log(firstName, lastName); // Ungmo Lee
ํ ๋น ๊ธฐ์ค์ด ํ๋กํผํฐ ํค
์ด๊ธฐ ๋๋ฌธ์ ์์๋ ์๋ฏธ๊ฐ ์๊ณ ์ ์ธ๋ ๋ณ์ ์ด๋ฆ๊ณผ ํ๋กํผํฐ ํค๊ฐ ์ผ์นํ๋ฉด ํ ๋น๋๋ค.
const { lastName, firstName } = { firstName: 'Ungmo', lastName: 'Lee' };
ํ ๋น ์ฐ์ฐ์ ์ผ์ชฝ์ ํ๋กํผํฐ ๊ฐ์ ํ ๋น๋ฐ์ ๋ณ์ ๊ฐ์ฒด ๋ฆฌํฐ๋ด ํํ๋ก ์ ์ธํด์ผ ํ๋ค.
const { lastName, firstName };
// SyntaxError: Missing initializer in destructuring declaration
const { lastName, firstName } = null;
// TypeError: Cannot destructure property 'lastName' of 'null' as it is null.
์ฐ๋ณ์ ๊ฐ์ฒด๋ฅผ ํ ๋นํ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค. ๊ฐ์ฒด๋ก ํ๊ฐ๋ ์ ์๋ ํํ์ ( ๋ฌธ์์ด, ์ซ์, ๋ฐฐ์ด ๋ฑ )๋ ํฌํจ๋๋ค.
const { lastName, firstName } = user;
// ์์ ์๋๋ ๋์น๋ค.
const { lastName: lastName, firstName: firstName } = user;
const user = { firstName: 'Ungmo', lastName: 'Lee' };
// ํ๋กํผํฐ ํค๋ฅผ ๊ธฐ์ค์ผ๋ก ๋์คํธ๋ญ์ฒ๋ง ํ ๋น์ด ์ด๋ฃจ์ด์ง๋ค.
// ํ๋กํผํฐ ํค๊ฐ lastName์ธ ํ๋กํผํฐ ๊ฐ์ ln์ ํ ๋นํ๊ณ ,
// ํ๋กํผํฐ ํค๊ฐ firstName์ธ ํ๋กํผํฐ ๊ฐ์ fn์ ํ ๋นํ๋ค.
const { lastName: ln, firstName: fn } = user;
console.log(fn, ln); // Ungmo Lee
๊ฐ์ฒด์ ํ๋กํผํฐ ํค์ ๋ค๋ฅธ ๋ณ์ ์ด๋ฆ์ผ๋ก ํ ๋น๋ฐ๋๋ค.
const { firstName = 'Ungmo', lastName } = { lastName: 'Lee' };
console.log(firstName, lastName); // Ungmo Lee
const { firstName: fn = 'Ungmo', lastName: ln } = { lastName: 'Lee' };
console.log(fn, ln); // Ungmo Lee
๊ธฐ๋ณธ๊ฐ์ ์ค์ ํด์ฃผ์๋ค. firstname
fn
, ln
const str = 'Hello';
// String ๋ํผ ๊ฐ์ฒด๋ก๋ถํฐ length ํ๋กํผํฐ๋ง ์ถ์ถํ๋ค.
const { length } = str;
console.log(length); // 5
const todo = { id: 1, content: 'HTML', completed: true };
// todo ๊ฐ์ฒด๋ก๋ถํฐ id ํ๋กํผํฐ๋ง ์ถ์ถํ๋ค.
const { id } = todo;
console.log(id); // 1
๊ฐ์ฒด์์ ํ๋กํผํฐ ํค๋ก ํ์ํ ํ๋กํผํฐ ๊ฐ๋ง ์ถ์ถํ์ฌ ๋ณ์์ ํ ๋นํ๊ณ ์ถ์ ๋ ์ ์ฉํ๋ค.
function printTodo(todo) {
console.log(`ํ ์ผ ${todo.content}์ ${todo.completed ? '์๋ฃ' : '๋น์๋ฃ'} ์ํ์
๋๋ค.`);
}
printTodo({ id: 1, content: 'HTML', completed: true });
// ํ ์ผ HTML์ ์๋ฃ ์ํ์
๋๋ค.
๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌ๋ฐ๋ ํจ์์ ๋งค๊ฐ๋ณ์์๋ ์ฌ์ฉํ ์ ์๋ค.
function printTodo({ content, completed }) {
console.log(`ํ ์ผ ${content}์ ${completed ? '์๋ฃ' : '๋น์๋ฃ'} ์ํ์
๋๋ค.`);
}
printTodo({ id: 1, content: 'HTML', completed: true });
// ํ ์ผ HTML์ ์๋ฃ ์ํ์
๋๋ค.
๊ฐ์ฒด๋ฅผ ์ธ์๋ก ์ ๋ฌ๋ฐ๋ ๋งค๊ฐ๋ณ์ todo
์ ๊ฐ์ฒด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น์ ์ฌ์ฉํ๋ฉด ๋ ๊ฐ๋จํ๊ณ ๊ฐ๋
์ฑ์ด ์ข๊ฒ ํํํ ์ ์๋ค.
const todos = [
{ id: 1, content: 'HTML', completed: true },
{ id: 2, content: 'CSS', completed: false },
{ id: 3, content: 'JS', completed: false }
];
// todos ๋ฐฐ์ด์ ๋ ๋ฒ์งธ ์์์ธ ๊ฐ์ฒด๋ก๋ถํฐ id ํ๋กํผํฐ๋ง ์ถ์ถํ๋ค.
const [, { id }] = todos;
console.log(id); // 2
๋ฐฐ์ด์ ์์๊ฐ ๊ฐ์ฒด์ผ ๋ ๋ฐฐ์ด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น๊ณผ ๊ฐ์ฒด ๋์คํธ๋ญ์ฒ๋ง ํ ๋น์ ํผ์ฉํ ์ ์๋ค.
const user = {
name: 'Lee',
address: {
zipCode: '03068',
city: 'Seoul'
}
};
// address ํ๋กํผํฐ ํค๋ก ๊ฐ์ฒด๋ฅผ ์ถ์ถํ๊ณ ์ด ๊ฐ์ฒด์ city ํ๋กํผํฐ ํค๋ก ๊ฐ์ ์ถ์ถํ๋ค.
const { address: { city } } = user;
console.log(city); // 'Seoul'
// Rest ํ๋กํผํฐ
const { x, ...rest } = { x: 1, y: 2, z: 3 };
console.log(x, rest); // 1 { y: 2, z: 3 }
Rest ํ๋กํผํฐ
...
๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
Rest ํ๋ผ๋ฏธํฐ
๋ Rest ์์
๊ฐ์ด ๋ฐ๋์ ๋งจ ๋ง์ง๋ง์ ์์นํด์ผ ํ๋ค.