πŸ“’[TypeScript λ§ˆμŠ€ν„° with Webpack & React] Step10.JavaScript 클래슀

κΆŒμš©μ€€Β·2023λ…„ 12μ›” 6일
0
post-thumbnail

1. Class μƒμ„±μž


μžλ°” μŠ€ν¬λ¦½νŠΈλŠ” μ›λž˜ ν΄λž˜μŠ€λŠ” μ—†μ—ˆκ³  ν”„λ‘œν† νƒ€μž… μ‹œμŠ€ν…œκ³Ό μƒμ„±μž ν•¨μˆ˜λ§Œ μžˆμ—ˆλ‹€.

μƒμ„±μž μ •μ˜λŠ” ν•„μˆ˜λŠ” μ•„λ‹ˆμ§€λ§Œ λŒ€κ°œ 클래슀λ₯Ό μ •μ˜ν•  λ–„ μ‚¬μš©ν•œλ‹€.
constructor라고 μ •μ˜ν•΄μ•Ό μžλ™μœΌλ‘œ ν˜ΈμΆœλœλ‹€.

class Player{
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!")
    }
    taunt(){
        console.log("YEAH!!")
    }
}

const player1 = new Player("blue", "steele");
console.log(player1.taunt()); // IN CONSTRUCTOR! YEAH!!
console.log(player1.first); // blue
console.log(player1.last); // steele

2. 클래슀 ν•„λ“œ


클래슀 ν•„λ“œλŠ” ν•„λ“œμ™€ ν”„λ‘œνΌν‹°λ₯Ό λΉ λ₯΄κ²Œ μ •μ˜ν•˜κ²Œ ν•΄μ£ΌλŠ” κ΅¬λ¬ΈμœΌλ‘œμ„œ 클래슀 μ•ˆμ—μ„œ 자유둭게 λ°”κΏ”κ°€λ©° μ‚¬μš©ν•  수 μžˆλ‹€.

class Player{
    score = 0; // class field
    numLives = 10; // class field
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!");
    }
    taunt(){
        console.log("YEAH!!");
    }
    loseLife(){
        this.numLives -= 1;
    }
}

const player1 = new Player("blue", "steele");
console.log(player1.numLives); // 10
plyaer1.loseLife();
console.log(player1.numLives); // 9

3. 프라이빗 ν•„λ“œ


프라이빗 ν•„λ“œλ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•΄μ„œλŠ” ν”„λ‘œνΌν‹°λ‚˜ ν•„λ“œ μ•žμ— # 기호λ₯Ό μΆ”κ°€ν•˜λ©΄ λœλ‹€.

프라이빗 ν•„λ“œλŠ” 클래슀 μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯ν•˜λ©° λ°–μ—μ„œλŠ” μ ‘κ·Όν•  수 μ—†λ‹€.


class Player{
    #score = 0; // private field  Player 클래슀 μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯
    numLives = 10; 
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!");
    }
    getScore(){
        return this.#score;
    }
    setScore(newScore){
        this.#score = newScore;
    }
}

const player1 = new Player("blue", "steele");
console.log(player1.#score); // error
console.log(player1.getScore); // 0
console.log(player1.setScore(28)); 
console.log(player1.getScore); // 28

4. Getters, Setters



class Player{
    #score = 0; // private field  Player 클래슀 μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯
    numLives = 10; 
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!");
    }
    get fullName(){ // getters
        return `${this.first} ${this.last}`
    }

    get score(){ // getters
        return this.#score; 
    }

    set score(newScore){ // setters 
        if(newScore < 0){
            throw new Error("Score must be positive!");
        }
        this.#score = newScore;
    }

}

const player1 = new Player("blue", "steele");
console.log(player1.score); // 0
console.log(player1.score(5)); 
console.log(player1.score); // 5

4. 정적 ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œ


νŠΉμ • μΈμŠ€ν„΄μŠ€μ™€ κ΄€λ ¨ μ—†μœΌλ©΄μ„œ 클래슀 μžμ²΄μ™€ μ—°κ΄€λœ κΈ°λŠ₯을 클래슀둜 κ·Έλ£Ήν™”ν•  수 μžˆκ²Œν•΄μ€€λ‹€.
즉, static은 ν”„λ‘œνΌν‹°λ‚˜ λ©”μ„œλ“œκ°€ ν΄λž˜μŠ€μ—λ§Œ μžˆλ‹€κ³  μ•Œλ €μ£ΌλŠ” 역할을 ν•œλ‹€.
주둜 생성 λ©”μ„œλ“œ ν˜Ήμ€ μƒˆλ‘œμš΄ μΈμŠ€ν„΄μŠ€λ‚˜ μ—¬λŸ¬ μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜λŠ” 헬퍼 등을 λ§Œλ“€λ•Œ 쓰인닀.


class Player{
    static description = "Player In Our Game";
    #score = 0; // private field  Player 클래슀 μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯
    numLives = 10; 
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!");
    }
    
    static randomPlayer(){
        return new Player("Andy", "Samberg");
    }
}

const player1 = new Player("blue", "steele");
console.log(player1.description); // undefined
console.log(Player.description); // Player In Our Game

5. 클래슀 ν™•μž₯(extends) 와 super


extends둜 κΈ°λ³Έ ν΄λž˜μŠ€μ™€ λΆ€λͺ¨ 클래슀λ₯Ό ν™•μž₯ ν•  수 μžˆλ‹€.

μƒμ„±μžκ°€ μ—¬λŸΏμ΄λΌλ©΄ super둜 λΆ€λͺ¨ μƒμ„±μžλ₯Ό λ¨Όμ € ν˜ΈμΆœν•œ λ‹€μŒ μ›ν•˜λŠ” 값을 전달해야 μžμ‹ ν΄λž˜μŠ€μ—μ„œ μ›ν•˜λŠ” 일을 ν•  수 μžˆλ‹€.


class Player{
    static description = "Player In Our Game";
    #score = 0; // private field  Player 클래슀 μ•ˆμ—μ„œλ§Œ μ‚¬μš©κ°€λŠ₯
    numLives = 10; 
    //μƒμ„±μž
    constructor(first, last){
        this.first = first;
        this.last = last;
        console.log("IN CONSTRUCTOR!")
    }
    
    static randomPlayer(){
        return new Player("Andy", "Samberg");
    }

    taunt(){
        console.log("YEAH!!")
    }
}

class AdminPlayer extends Player{ // 클래슀 ν™•μž₯
    constructor(first, last, powers){
        super(first,last) // Player constructorλ₯Ό 의미
        this.powers = powers
    }
    isAdmin = true;
}

const admin = new AdminPlayer("admin", "mCadmin", ["delete", "restore"]);
profile
Brendan Eich, Jordan Walke, Evan You, κΆŒμš©μ€€

0개의 λŒ“κΈ€