GRAMMAR

Record
  • account_tree
  • bug_report

구조 분해 할당 구문(Destructuring assignment syntax)

ECMAScript 6(2015)

구조 분해 할당 구문은 배열이나 객체의 속성을 해체하여 그 값을 개별 변수에 담을 수 있는 새로운 표현식이다.

구문 예시 및 샘플 코드

배열에서 구조화 분해 할당 - 변수

let a, b;
// Destructuring assignment
[a, b] = ['Hi', 'Hello'];
console.log(a); // 'Hi';
console.log(b); // 'Hello';

배열에서 구조화 분해 할당 - 배열

let a, b, rest;
// Destructuring assignment
[a, b, ...rest] = ['Hi', 'Hello', 10, 20, 30];
console.log(a); // 'Hi';
console.log(b); // 'Hello';
console.log(rest); // [10, 20, 30];

객체에서 구조화 분해 할당 - 변수

let a, b;
// Destructuring assignment
({ a, b } = { a: "Hi", b: "Hello" });
console.log(a); // 'Hi';
console.log(b); // 'Hello';

객체에서 구조화 분해 할당 - 객체

let a, b, rest;
// Destructuring assignment
({ a, b, ...rest } = { a: "Hi", b: "Hello", c: 10, d: 20 });
console.log(a); // 'Hi';
console.log(b); // 'Hello';
console.log(rest); // { c: 10, d: 20 }