ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JS 100์ œ] ๋ฌธ์ œ12 - ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ
    ๐Ÿ›  develop/CodingTest 2022. 10. 19. 21:03

    ๋ฌธ์ œ

    ๋‹ค์Œ ์†Œ์Šค์ฝ”๋“œ์—์„œ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ์˜ ๋Šฅ๋ ฅ์น˜์™€ 'ํŒŒ์ด์–ด๋ณผ'์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๋งŒ๋“œ์‹œ์˜ค. ์ฃผ์–ด์ง„ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์„  ์•ˆ๋ฉ๋‹ˆ๋‹ค.

    ๋ฐ์ดํ„ฐ
    <์—ฌ๊ธฐ์— class๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.>
    
    const x = new Wizard(545, 210, 10);
    console.log(x.health, x.mana, x.armor);
    x.attack();
    
    ์ถœ๋ ฅ
    545 210 10
    ํŒŒ์ด์–ด๋ณผ

     

    ๋‚˜์˜ ํ’€์ด

    class Wizard {
      constructor(a, b, c) {
        this.health = a;
        this.mana = b;
        this.armor = c;
      }
      attack() {
        console.log("ํŒŒ์ด์–ด๋ณผ");
      }
    }
    
    const x = new Wizard(545, 210, 10);
    console.log(x.health, x.mana, x.armor);
    x.attack();
    
    // ์ถœ๋ ฅ
    545 210 10
    ํŒŒ์ด์–ด๋ณผ
    1. class ์„ ์–ธ์‹์„ ์ด์šฉํ•˜์—ฌ Wizard ๋ผ๋Š” class ๋ฅผ ์ •์˜ํ•ด์ฃผ์—ˆ๋‹ค.
    2. constructor ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ a, b, c ๋ฅผ ์ฃผ์–ด ์„ธ๊ฐœ์˜ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€๋‹ค.
    3. this.health = a; ์™€ ๊ฐ™์ด ๊ฐ’์„ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.
    4. attack ํ•จ์ˆ˜๋„ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์˜ํ•˜์—ฌ ์ถœ๋ ฅ์— ๋งž๊ฒŒ ์ž‘์„ฑํ•˜์˜€๋‹ค.

     

    ์ •๋‹ต

    const Wizard = class Wizard {
        constructor (health, mana, armor){
            this.health = health;
            this.mana = mana;
            this.armor = armor;
        }
        attack(){
            console.log('ํŒŒ์ด์–ด๋ณผ');
        }
    }
    
    const x = new Wizard(545, 210, 10);
    console.log(x.health, x.mana, x.armor);
    x.attack();

    ๋‹ต์•ˆ์€ ํ‘œํ˜„์‹์œผ๋กœ class๋ฅผ ์„ ์–ธํ•ด์ฃผ์—ˆ๊ณ , ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์˜ ์ด๋ฆ„๋„ ๋™์ผํ•˜๊ฒŒ ์ง€์ •ํ•ด ์ค€ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ฐจํ›„์— class ๋ฅผ ์„ ์–ธํ•˜๊ฒŒ ๋˜๋ฉด ํ‘œํ˜„์‹์œผ๋กœ ์ •์˜ํ•ด๋‘๋Š” ๊ฒƒ์ด ์žฌ์‚ฌ์šฉ์„ฑ์— ์šฉ์ดํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ,, ๋‹ค์Œ๋ถ€ํ„ด ๋ฌธ์ œ๋ผ๋„ ๊ทธ๋ ‡๊ฒŒ ํ’€์–ด๋ณด์ž ์ƒ๊ฐํ–ˆ๋‹ค.

     

    JS ๊ฐ„๋‹จ ๋ณต์Šต

    JavaScript Class ์— ๋Œ€ํ•ด์„œ

     

    Class

    class๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํ…œํ”Œ๋ฆฟ์ด๋‹ค. class๋Š” ๋ฐ์ดํ„ฐ์™€ ์ด๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜๋กœ ์ถ”์ƒํ™”ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

     

    Class ๋ฌธ๋ฒ•

    class ๋ฌธ๋ฒ•์€ class ์„ ์–ธ๊ณผ class ํ‘œํ˜„์‹ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค. 

     

    Class ์„ ์–ธ

    ํด๋ž˜์Šค์˜ ์ด๋ฆ„(์˜ˆ์ œ์—์„  Rectangle)๊ณผ ํ•จ๊ป˜ class ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ ์–ธํ•œ๋‹ค. class ๋‚ด๋ถ€์˜ constructor ๋Š” ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์œ ๋Š” constructor ๊ฐ€ ์ƒ์„ฑ์ž ๊ทธ ์ž์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ฆ‰, ์•„๋ž˜์˜ ์˜ˆ์‹œ๋กœ ๋ณด๋ฉด constructor ๋Œ€์‹  Rectangle์„ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    class Rectangle {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
    }

     

    Class ํ‘œํ˜„์‹

    ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ทธ ๋ณ€์ˆ˜์— class๋ฅผ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์„ class ํ‘œํ˜„์‹์ด๋ผ๊ณ  ํ•œ๋‹ค. ์ด๋Š” class๋ฅผ ์ •์˜ํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด๋‹ค.

    class ํ‘œํ˜„์‹์€ ์ด๋ฆ„์„ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฐ–์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฆ„์„ ๊ฐ€์ง„ class ํ‘œํ˜„์‹์˜ ์ด๋ฆ„์€ ํด๋ž˜์Šค body์˜ local scope์— ํ•œํ•ด ์œ ํšจํ•˜๋‹ค.

    // unnamed
    let Rectangle = class {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
    };
    console.log(Rectangle.name);
    // ์ถœ๋ ฅ: "Rectangle"
    
    // named
    let Rectangle = class Rectangle2 {
      constructor(height, width) {
        this.height = height;
        this.width = width;
      }
    };
    console.log(Rectangle.name);
    // ์ถœ๋ ฅ: "Rectangle2"

     

    ์ถ”๊ฐ€์ ์œผ๋กœ, ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ํด๋ž˜์Šค ์„ ์–ธ์˜ ์ฐจ์ด๋Š” ๋ฐ”๋กœ ํ˜ธ์ด์ŠคํŒ…์ด๋‹ค.

    ํ•จ์ˆ˜๋Š” ์ •์˜ํ•˜๊ธฐ ํ•˜๊ธฐ ์ „์— ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ํด๋ž˜์Šค๋Š” ์ •์˜ํ•˜๊ธฐ ์ „์— ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค. ์ฆ‰ ๋ฐ˜๋“œ์‹œ ์ •์˜ ํ›„์— ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

    ๋Œ“๊ธ€