class Student{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
}
constructor
를 사용해 원하는 인스턴스를 생성, 초기화 수 있음new
키워드를 사용 let firstStudent = new Student("Colt", "Steele");
let secondStudent = new Student("Blue", "Steele");
firstStudent
는 student 클래스의 인스턴스, firstStudent
의 성은 Colt, 이름은 Steele이 됨class Student{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
fullName(){
return `Your full name is ${this.firstName} ${this.lastName}`;
}
}
let firstStudent = new Student("Colt", "Steele");
firstStudent.fullName(); // "Colt Steele"
fullName()
이 인스턴스 메소드에 해당static
키워드static
은 클래스 내 정적 메소드를 의미class Student{
constructor(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
fullName(){
return `Your full name is ${this.firstName} ${this.lastName}`;
}
static EnrollStudents(){
return "ENROLLING STUDENTS";
}
}
let firstStudent = new Student("Colt", "Steele");
firstStudent.EnrollStudents(); // error
Student.EnrollStudents(); // "ENROLLING STUDENTS"
static
키워드를 사용한 클래스 메소드는 개별 인스턴스와는 무관한 기능this
this
는 클래스로부터 생성된 객체, 즉 실제 인스턴스를 참조