객체 비구조/구조분해 문법

Clém·2020년 12월 22일
0
post-thumbnail

함수를 정의할 때 구조분해 문법과 비구조 할당에 대해서 알아보자.

코드를 입력하세요const sonny = {
  name: '손흥민',
  position: 'forward',
  backNumber: 7
};

const kane = {
  name: '해리 케인',
  position: 'striker',
  backNumber: 10
};

function print(footballer) {
  const text = `${footballer.name}(${footballer.backNumber})은 Spurs의 ${footballer.position}이다. `;
  console.log(text);
}

print(sonny);
print(kane);

객체의 Key를 하나의 객체로 담아 정의할 수 있다.

function print(footballer) {
  const { name, position, backNumber } = footballer;
  const text = `${name}(${backNumber})은 Spurs의 ${position}이다. `;
  console.log(text);
}

또 다른 방법으로 함수의 파라미터 안에 객체로 넣어 사용할 수도 있다.

function print({ name, position, backNumber }) {
  const text = `${name}(${backNumber})은 Spurs의 ${position}이다. `;
  console.log(text);
}

이처럼 객체구조 분리와 비구조 할당을 할 수 있다.

profile
On my way to become a Web Publisher

0개의 댓글