[JavaScript] 비구조화 할당

유얌얌·2024년 2월 20일
0

JavaScript

목록 보기
14/30

🥑 배열의 비구조화 할당

// swipe
let a = 10
let b = 20

[a, b] = [b, a];
console.log(a, b) // 20 10

🥑 객체의 비구조화 할당

key값으로 매칭 !

let obj = { one: 1, two: 2, three:3 }

let { one, two, three } = obj

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


// 다른 이름으로 할당받기
let { one:hi, two, three } = obj
console.log(hi, two, three) // 1, 2, 3


// 기본값 할당
let { one:hi, two, three, abc=4 } = obj
console.log(hi, two, three, abc) // 1, 2, 3, 4

🥑 함수의 구조분해 할당

let obj = {
  name: "포도",
  age: 12,
  hobby: "eating",
};

let { name: myname, age, hobby, etc = "hi" } = obj;

console.log(myname, age, hobby, etc);

const func = ({ name, age, hobby }) => {
  console.log(name, age, hobby);
};

func(obj);
profile
조금씩이라도 꾸준하게

0개의 댓글

관련 채용 정보