// es6문법(2015)
// 대규모 문법적 향상
// 1. let(변수), const(상수) 추가
// 2. arrow function
let add = (a,b) => {
return a+b
}
// 3. 삼항 연산자
// condition ? true인 경우 : fales인 경우
// 구조분해할당
// destructuring (de + structure + ing )
// 배열이나, 객체의 구조을 분해해서 그 속성을 하나 하나의 변수에 할당한다
// 1. 배열인 경우
let arr = ["1", "2", "3"];
let [a, b, c] = arr;
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
// Q. 변수가 더 많은 경우
let [a2, b2, c2, d2] = arr;
console.log(d2); // undefined
// 문제 해결 - 초기값 할당
let [a3, b3, c3, d3 = "초기값"] = arr;
console.log(d3); // 초기값
// Q. const는 재할당이 불가능한데 로 초기값을 준 상태에서 구조분해 할당을 한다면?
const [a4, b4, c4, d4 = "초기값"] = ["01", "02", "03", "04"];
console.log(d4);
// A. 구조분해할당의 초기값은 선언부터 할당되는 것이 아니라
// 분해 할당이 이루어진 이후에
// 주어진 값이 없다면 그 때 할당되는 것 같다
// 2. 객체인 경우
let person = {
name : "mjm",
age : 30
};
let { name, age } = person;
console.log(name); // mjm
console.log(age); // 30
console.log(typeof name); // string
console.log(typeof age); // 30
//구조분해할당을 해서 객체타입이 아니다
// 새로운 이름으로 할당
let {name : newname, age : newage} = person;
console.log(newname);
console.log(newage);
// 객체도 배열처럼 변수가 더 많다면 undefined
// 이를 위한 초기값 설정이 가능하다
느낌적으로 구조분해할당은 많이 써먹을 거 같다
'오늘 뭐했냐 > 개발에 대한 주저리' 카테고리의 다른 글
일급 객체 객체로서의 함수 (1) (0) | 2023.05.24 |
---|---|
es6(2) (0) | 2023.05.24 |
for문 (0) | 2023.05.23 |
배열(콜백함수o) (0) | 2023.05.23 |
배열(콜백함수x) (0) | 2023.05.23 |