Algorithm

Class Method 간단 설명

주인장 꼬비 2022. 9. 1. 21:33

- Class 들은 instance 로 알려진 객체를 생성하기 위한 청사진

- 이런 Class 들은 "new" 키워드를 통해 생성되거나 인스턴스화된다.

- constructor() (constructor fucntion) 은 class 가 instance화 될 때 동작하는 특별한 function이다.

- "new" 를 통해 "Student" 클래스를 instance화 시키게 되면 "Student"의 constructor가 먼저 동작하게 된다.

- instance method 는 method 혹은 객체와 유사한 방식으로 클래스에 추가될 수 있으며, class method는 "static" 키워드 와 함께 추가될 수 있다.

class Student {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  
  fullName() {
    return `Your full name is ${this.firstName} ${this.lastName}`;
  }
  
  static enrollStudent(...students) {
    // maybe send an email here
  }
}

let firstStudent = new Student("Colt", "Steele");
let secondStudent = new Student("Blue", "Steele");

Student.enrollStudent([firstStudent, secondStudent]);