Static Keyword

wonyoung·2024년 1월 30일

Javascript

목록 보기
6/36
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 키워드 안쓰는 이유

factory constructor

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);

강의출처 https://youtu.be/ZOVG7_41kJE?si=Gao3ZNE0CzZhpRFv

profile
😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀

0개의 댓글