9.2.8 문자열 표현

toString() 메서드

  • 기본 동작 : "[object Object]" => 쓸모 없음

  • toString 메서드에서 객체에 관한 중요한 정보를 제공할 수 있도록 수정해야함

  class Car {
    constructor(make, model) {
      this.make = make;
      this.model = model;
    }

    toString() {
      return `${this.make} ${this.model}`;
    }
  }

  const car1 = new Car("Hyundae", "G70");
  const car2 = new Car("KIA", "ray");

  console.log(car1.toString()); // Hyundae G70
  console.log(car2.toString()); // KIA ray

Last updated