객체를 한번에 받아오는 방법. 객체 구조 분해라고도 불리운다.
즉
const apple = {name: apple, weight: 10, color: red};
const peach = {name: peach, weight: 5, color: pink};
function print(fruit){
const {name, weight, color} = fruit;
const a = `${name}의 무게는 ${weight}이고 색깔은 ${color}입니다.`;
console.log(a)
}
print(apple)을 넣으면, apple 내부의 값이 분해되어 저장된다.
apple의 name, weight, color가 쭉쭉 분해됨. 지역변수로 저장되어 1회성으로 사용된다.
const apple = {name: apple, weight: 10, color: red};
const peach = {name: peach, weight: 5, color: pink};
function print({name, weight, color}){
const a = `${name}의 무게는 ${weight}이고 색깔은 ${color}입니다.`;
console.log(a)
}
이렇게 직접적으로 넣을 수도 있다. (함수의 파라미터에 직접 할당)
두번째가 더 편하다.
객체에 할 수 있고, 배열에도 할 수 있다.