문법
class ClassName{ 속성명 = 초기값; // 초기값 설정 constructor() {...} // 생성자 함수 // === Setter === // set xxx(value) { this.속성명 = value; } // === Getter === // get xxx(){return this.속성명;} // === method === // method(){...} }
속성명 = 0;
#속성명 = 0;
constructor(필드명1,필드명2){
this.필드명1 = 필드명1;
this.필드명2 = 필드명2;
}
set set속성명(value){ // 이때 속성명의 맨 앞의 글자는 대문자
...
this.#속성명 = value;
}
get get속성명(){ // 이때 속성명의 맨 앞의 글자는 대문자
return this.#속성명;
}
Setter 주의
잘못된 경우 => 객체.set속성명(value);
올바른 방법 => 객체.set속성명 = "value";
Getter 주의
잘못된 경우 => 객체.get속성명()
올바른 방법 => 객체.get속성명
// 함수 선언
함수명(){
const currentDate = new Date(); // 현재 날짜 시간
// currentDate.getFullYear() : 현재 년도
return currentDate.getFullYear() - this.#속성명 + 1; // 나이
}