코드팩토리 JS

15. static keyword

박성하하 2025. 12. 26. 20:33
728x90
반응형
/**
 * Static Keyword
 */
// class IdolModel {
//     name;
//     year;
//     static groupName = '아이브';

//     constructor(name, year) {
//         this.name = name;
//         this.year = year;
//     }

//     static returnGroupName(){
//         return '아이브';
//     }
// }

// const yuJin = new IdolModel('안유진', 2003);
// console.log(yuJin);

// console.log(IdolModel.groupName);
// console.log(IdolModel.returnGroupName());

/**
 * factory constructor
 */
class IdolModel{
    name;
    year;

    constructor(name, year){
        this.name = name;
        this.year = year;
    }

    static fromObject(object){
        return new IdolModel(
            object.name,
            object.year,
        );
    }

    static fromList(list){
        return new IdolModel(
            list[0],
            list[1],
        );
    }
}

const yuJin2 = IdolModel.fromObject({
    name: '안유진',
    year: 2003,
});
console.log(yuJin2);            // IdolModel {name: '안유진', year: 2003}

const wonYoung = IdolModel.fromList(
    [
        '장원영',
        2003,
    ]
);
console.log(wonYoung);          // IdolModel {name: '장원영', year: 2003}
728x90
반응형

'코드팩토리 JS' 카테고리의 다른 글

17. super , override  (0) 2025.12.26
16. inheritance  (0) 2025.12.26
14. Getter and Setter  (0) 2025.12.26
13. class  (0) 2025.12.26
12. try catch  (0) 2025.12.26