[구조분해할당] js_ ... 변수 선언

BABY CAT·2022년 10월 4일
0

HTML, CSS, JavaScript

목록 보기
12/23

배열[인덱] 구조분해할당

var array = 배열
var node = array[인덱]

... 선언

let a, b, rest2;
[a, b, ...rest2] = [10, 20, 30, 40, 50];
console.log(rest2);
// expected output: Array [30,40,50]
rest2 이 [ 30, 40, 50 ]  으로 선언
a = 10
b = 20

( ); 선언

({ a, b } = { a: 10, b: 20 });
console.log(a); // 10
console.log(b); // 20

( ); 선언 강화

({a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40});
console.log(a); // 10
console.log(b); // 20
console.log(rest); // {c: 30, d: 40}

구조분해할당

var [y, z] =  [1, 2, 3, 4, 5];
console.log(y); // 1
console.log(z); // 2

구조분해할당_다중 기본변수할당

var [red, yellow, green] = ["one", "two", "three"];
console.log(red); // "one"
console.log(yellow); // "two"
console.log(green); // "three"

선언에서 분리한 할당

var a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7

변수 교환하기

var a = 1;
var b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1

함수가 반환한 배열 분석

function f() {
   return [1, 2];
}
var a, b;
[a, b] = f();
 console.log(a); // 1
 console.log(b); // 2

일부 반환 값 무시하기

function f() {
   return [1, 2, 3];
 }
 var [a, , b] = f();
 console.log(a); // 1
 console.log(b); // 3

반환 값 모두 무시

function f() {
   return [1, 2, 3];
 }
 [,,] = f();

변수에 배열의 나머지를 할당하기

var [a, ...b] = [1, 2, 3];
console.log(a); // 1
console.log(b); // [2, 3]
나머지 요소의 오른쪽 뒤에 쉼표가 있으면 SyntaxError가 발생합니다.
var [a, ...b,] = [1, 2, 3];
	SyntaxError: rest element may not have a trailing comma

//## 정규 표현식과 일치하는 값 해체하기

0개의 댓글