본문 바로가기
모바일 APP/React-Native

ES6 구조 분해와 구조 할당

by 살길바라냐 2021. 12. 9.

기본값 할당

ES5 예제

const list = [0,1];
const item1 = list[0];
const item2 = list[1];

// 기본값 할당
const item3 = list[2] || -1;

ES6 예제

const list = [0, 1];
const [
  item1,
  item2,
  
  // 기본값 할당
  item3 = -1,
] = list;

 

변수를 교차하여 배열의 두값을 치환

ES5 예제

var temp = item2;
item2= item1;
item1 = temp;

 

ES6 예제

[item2, item1] = [item1, item2];

 

객체 기본값 할당

 

ES5 예제

const key1 = obj.key1;
const key2 = obj.key2;
const key3 = obj.key3 || 'default key3 value';
const newKey1 = key1;

 

ES6 예제

var {
  key1: newKey1,
  key2,
  key3 = 'default key3 value',
} = obj;

 

전개연산자 

const [item1, ...otherItems] = [0, 1, 2];
// otherItems = [1, 2]

const { key1, ...others } = { key1: 'one', key2: 'two' };
// others = { key2: 'two' }

 

728x90
반응형

'모바일 APP > React-Native' 카테고리의 다른 글

리액트 네이티브란?  (0) 2022.02.24
아이폰 안드로이드 디스플레이 길이  (0) 2021.12.27
ES6 화살표 함수  (0) 2021.12.09
가변 변수와 불변 변수  (0) 2021.12.09
전개 연산자  (0) 2021.12.07