class Model{
name;
year;
static groupName = '노원구' // statice은 객체에 귀속되지 않는다. class자체에 귀속된다.
constructor(name, year){
this.name = name;
this.year = year;
}
static returnGroupName(){
return '노원구'
}
}
const wonYoung = new Model('이원영', 1997);
console.log(wonYoung); // 그룹네임 미출력
console.log(Model.groupName); // 노원구
console.log(Model.returnGroupName()) // 노원구
static키워드는 class 자체에 귀속된다. 객체에 귀속x = new 키워드 안쓰는 이유
class Model2 {
name;
year;
constructor(name, year) {
this.name = name;
this.year = year;
}
static fromObject(object) {
return new Model2(
object.name,
object.year,
);
}
static fromList(list) {
return new Model2(
list[0],
list[1],
);
}
}
const wonYoung2 = Model2.fromObject({
name: '이원영',
year: 1997,
});
console.log(wonYoung2);
const wonYoung3 = Model2.fromList(
[
'이원영',
1997,
]
)
console.log(wonYoung3);