javaScript

오브젝트 리터럴 클래스로 매개변수 전달하기

mooyou 2023. 6. 8. 23:22
728x90
300x250
SMALL

 

아래는 매개 변수 값을 이용해 함수를 출력하는 예시이다.

여러 개의 메서드를 불러올 경우 일반적으로 매개변수 위치를 맞춰서 전달해야 한다.

function printInfo(name, age, location) {
  document.write(`Name: ${name}<br>`);
  document.write(`Age: ${age}<br>`);
  document.write(`Location: ${location}<br>`);
}

printInfo("John",30,"New York");

실행결과

Location: New York

Name: John
Age: 30

 

 

 

이번에는 위의 예제를 오브젝트 리터를 클래스 매개변수로 전달하는 예시이다.

오브젝트 리터럴 클래스로 묶어서 매개변수를 전달하면 순서가 변경되더라도 상관이 없다.

function printInfo(person) {
  document.write(`Name: ${person.name}<br>`);
  document.write(`Location: ${person.location}<br>`);
  document.write(`Age: ${person.age}<br>`);
}

const person = {
    name:"John",
    age:30,
    location:"New York"
}

printInfo(person);

 

실행결과

Name: John
Location: New York
Age: 30

728x90
반응형
LIST