• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

타입스크립트 교과서, p131

24.05.03 16:16 작성 24.05.03 16:17 수정 조회수 92

0

여기서 Person3 결과로
Person: {

"name: "zero",

"age": 28,

married: false

}

이렇게 나와야 하는거 아닌가요??

전체 코드입니다.

class Person {
  name?: string;
  age?: number;
  married?: boolean;

  constructor();
  constructor(name: string, married: boolean);
  constructor(name: string, age: number, married: boolean);
  constructor(name?: string, age?: boolean | number, married?: boolean) {
    if (name) {
      this.name = name;
    }

    if (typeof age === "boolean") {
      this.married = age;
    } else {
      this.age = age;
    }

    if (married) {
      this.married = married;
    }
  }
}

const person1 = new Person();
const person2 = new Person("nero", true);
const person3 = new Person("zero", 28, false);

 

답변 1

답변을 작성해보세요.

1

코드 보시면 married가 true일 때만 this.married를 설정하도록 되어있습니다.