ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น(destructuring assignment, ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)์€ ๊ตฌ์กฐํ™”๋œ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ๋˜๋Š” ๊ฐ์ฒด๋ฅผ destructuring(๋น„๊ตฌ์กฐํ™”, ๊ตฌ์กฐ ํŒŒ๊ดด)ํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ๊ฐœ๋ณ„์ ์œผ๋กœ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ” ๋˜๋Š” ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์—์„œ ํ•„์š”ํ•œ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น

๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ, ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ์˜ ์šฐ๋ณ€)์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์ด์–ด์•ผ ํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ž…๋‹ˆ๋‹ค.

 

// ES6
const arr = [1, 2, 3];

// ES6 ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
// ๋ณ€์ˆ˜ one, two, three๋ฅผ ์„ ์–ธํ•˜๊ณ  ๋ฐฐ์—ด arr์„ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋งํ•˜์—ฌ ํ• ๋‹นํ•œ๋‹ค.
// ์ด๋•Œ ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋‹ค.
const [one, two, three] = arr;

console.log(one, two, three); // 1 2 3

// 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 ํ‚ค์›Œ๋“œ๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๊ถŒ์žฅ X
  • ํ• ๋‹น์˜ ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ผ ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น๋จ (๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜์™€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์š”์†Œ ๊ฐœ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์•„๋„ ๋จ)
  • ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ Rest ์š”์†Œ ... ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ (Rest ์š”์†Œ๋Š” ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰์— ์œ„์น˜)
// 1. ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ํ• ๋‹น
const [x, y] = [1, 2];

// 2. ์šฐ๋ณ€์— ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ํ• ๋‹นํ•ด์•ผ ํ•จ
const [x, y]; // SyntaxError: Missing initializer in destructuring declaration

const [a, b] = {}; // TypeError: {} is not iterable

// 3. ๋ณ€์ˆ˜ ์„ ์–ธ์™€ ํ• ๋‹น ๋ถ„๋ฆฌ
let x, y;
[x, y] = [1, 2];

// 4. ํ• ๋‹น์˜ ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ผ ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น๋จ
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

// 5. ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
// ๊ธฐ๋ณธ๊ฐ’
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

// 6. ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— Rest ์š”์†Œ ... ์„ ์‚ฌ์šฉ
// Rest ์š”์†Œ
const [x, ...y] = [1, 2, 3];
console.log(x, y); // 1 [ 2, 3 ]

 

์ด์ฒ˜๋Ÿผ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์—์„œ ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด URL์„ ํŒŒ์‹ฑํ•˜์—ฌ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์œผ๋กœ protocol, host, path ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

// 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'
}
*/

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น

๊ฐ์ฒด์˜ ๊ฐ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ 1๊ฐœ ์ด์ƒ์˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ, ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ(ํ• ๋‹น๋ฌธ์˜ ์šฐ๋ณ€)์€ ๊ฐ์ฒด์ด์–ด์•ผ ํ•˜๋ฉฐ, ํ• ๋‹น ๊ธฐ์ค€์€ ํ”„๋กœํผํ‹ฐ ํ‚ค์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ˆœ์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฉฐ ์„ ์–ธ๋œ ๋ณ€์ˆ˜ ์ด๋ฆ„๊ณผ ํ”„๋กœํผํ‹ฐ ํ‚ค๊ฐ€ ์ผ์น˜ํ•˜๋ฉด ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

 

// ES6
const user = { firstName: 'Ungmo', lastName: 'Lee' };

// ES6 ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น
// ๋ณ€์ˆ˜ lastName, firstName์„ ์„ ์–ธํ•˜๊ณ  user ๊ฐ์ฒด๋ฅผ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋งํ•˜์—ฌ ํ• ๋‹นํ•œ๋‹ค.
// ์ด๋•Œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์ด ์ด๋ฃจ์–ด์ง„๋‹ค. ์ˆœ์„œ๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค.
const { lastName, firstName } = user;

console.log(firstName, lastName); // Ungmo Lee

// ES5
var user = { firstName: 'Ungmo', lastName: 'Lee' };

var firstName = user.firstName;
var lastName  = user.lastName;

console.log(firstName, lastName); // Ungmo Lee

 

๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • ํ• ๋‹น ์—ฐ์‚ฐ์ž ์™ผ์ชฝ์— ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ํ• ๋‹น๋ฐ›์„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ํ˜•ํƒœ๋กœ ์„ ์–ธํ•ด์•ผ ํ•จ
  • ์šฐ๋ณ€์— ๊ฐ์ฒด ๋˜๋Š” ๊ฐ์ฒด๋กœ ํ‰๊ฐ€๋  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์‹(๋ฌธ์ž์—ด, ์ˆซ์ž, ๋ฐฐ์—ด)์„ ํ• ๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ
  • ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Œ
  • ๊ฐ์ฒด์—์„œ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ ์œ ์šฉํ•จ
  • ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ํ• ๋‹น์„ ์œ„ํ•œ ๋ณ€์ˆ˜์— Rest ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ Rest ์š”์†Œ ... ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ (Rest ์š”์†Œ๋Š” ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰์— ์œ„์น˜)
// 1. ๋ณ€์ˆ˜ ์„ ์–ธ
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.

// 2. ๊ธฐ๋ณธ๊ฐ’ ์„ค์ •
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

// 3. ํ‚ค๋กœ ๊ฐ’ ์ถ”์ถœ
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

// 4. ๊ฐ์ฒด๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ํ™œ์šฉ
function printTodo({ content, completed }) {
  console.log(`ํ• ์ผ ${content}์€ ${completed ? '์™„๋ฃŒ' : '๋น„์™„๋ฃŒ'} ์ƒํƒœ์ž…๋‹ˆ๋‹ค.`);
}

printTodo({ id: 1, content: 'HTML', completed: true });
// ํ• ์ผ HTML์€ ์™„๋ฃŒ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.

// 5. Rest ํ”„๋กœํผํ‹ฐ
const { x, ...rest } = { x: 1, y: 2, z: 3 };
console.log(x, rest); // 1 { y: 2, z: 3 }

 

๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น๊ณผ ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์„ ํ˜ผ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

// 1. ํ˜ผ์šฉ ์‚ฌ์šฉ
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

// 2. ์ค‘์ฒฉ ๊ฐ์ฒด
const user = {
  name: 'Lee',
  address: {
    zipCode: '03068',
    city: 'Seoul'
  }
};

// address ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ๊ฐ์ฒด๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์ด ๊ฐ์ฒด์˜ city ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค.
const { address: { city } } = user;
console.log(city); // 'Seoul'

[์ถœ์ฒ˜] ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive

https://wikibook.co.kr/mjs/

 

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ

269๊ฐœ์˜ ๊ทธ๋ฆผ๊ณผ ์›๋ฆฌ๋ฅผ ํŒŒํ—ค์น˜๋Š” ์„ค๋ช…์œผ๋กœ ‘์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ’๋ฅผ ์ดํ•ดํ•˜์ž! ์›นํŽ˜์ด์ง€์˜ ๋‹จ์ˆœํ•œ ๋ณด์กฐ ๊ธฐ๋Šฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ œํ•œ์ ์ธ ์šฉ๋„๋กœ ํƒœ์–ด๋‚œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ณผ๋„

wikibook.co.kr