자바스크립트 클래스

joyoung·2024년 1월 7일
0

클래스

class Profile {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  introduce() {
    return `안녕하세요, ${this.age}세 ${this.name}입니다!`;
  }
}

const people = new Profile('짱구', 5);

console.log(people);

constructor 메서드

constructor를 사용하면 다른 모든 메서드 호출보다 앞선 시점인,
인스턴스 객체를 초기화할 때 수행할 초기화 코드를 정의할 수 있음
클래스 선언시 하나만 있을 수 있음
다른 메서드 이름을 쓸 수 없음
선언해주지 않아도 괜찮음
클래스도 사실 함수임

profile
꾸준히

0개의 댓글

관련 채용 정보