๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Javascript

[JS] ํด๋ž˜์Šค(Class) ๊ธฐ๋ณธ ๋ฌธ๋ฒ•๊ณผ ์ƒ์†

by Fomagran ๐Ÿ’ป 2022. 2. 12.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์ด๋ฒˆ ๊ธ€๋„ ์—ญ์‹œ ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฐฐ์šด 'ํด๋ž˜์Šค'๋ฅผ ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋ฐฉ์‹์œผ๋กœ ๋ณต์Šตํ•˜๋Š” ์ฐจ์›์—์„œ

 

์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


ํด๋ž˜์Šค๋ž€?

 

ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํŠน์ • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ์ผ์ข…์˜ ํ‹€๋กœ, ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ ์ƒํƒœ(๋ฉค๋ฒ„ ๋ณ€์ˆ˜)์™€ ๋ฉ”์„œ๋“œ(ํ•จ์ˆ˜)๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. - ์œ„ํ‚ค ๋ฐฑ๊ณผ -

 

์ฆ‰, ํด๋ž˜์Šค๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์‰ฝ๊ฒŒ ๋ฏธ๋ฆฌ ํ‹€์„ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.


ํด๋ž˜์Šค ์ƒ์„ฑํ•˜๊ธฐ

 

์•ž์— ํด๋ž˜์Šค๋ฅผ ๋ถ™์—ฌ์ค€ ๋’ค constructor(์ƒ์„ฑ์ž)๋ฅผ ์ด์šฉํ•ด์„œ ์ดˆ๊ธฐํ™” ํ•ด์ค€๋‹ค.

 

๋ฉ”์„œ๋“œ๋‚˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฒ•์€ ์ผ๋ฐ˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ์™€ ๋™์ผํ•˜๋‹ค.

 

 class Player {
    constructor(name) {
      this.height = 180
      this.name = name
    }

    jump(space) {
      this.height += space
      console.log(`${this.name}์ด ์ ํ”„๋ฅผ ํ•˜์—ฌ ${this.height}๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.`)
    }

    run() {
      console.log(`${this.name}์ด ๋›ฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.`)
    }
  }

ํด๋ž˜์Šค ์ƒ์†๋ฐ›๊ธฐ

 

ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋ฐฉ๋ฒ•์€ ํด๋ž˜์Šค ์ด๋ฆ„ ๋’ค์— extends๋ฅผ ๋ถ™์ด๊ณ  ์ƒ์†๋ฐ›์„ ํด๋ž˜์Šค ๋ฅผ ์ฑ„ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“  ๊ฐ์ฒด์™€ ๊ฐ™์ด ๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 //extends๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์†๋ฐ›์Œ
  class BasketBallPlayer extends Player {
    shoot() {
      console.log("Shoot")
    }
  }
  //BasketBallPlayer๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์—†๋”๋ผ๋„ ๋ถ€๋ชจ๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€๋Šฅํ•˜๋‹ค.
  let basket = new BasketBallPlayer("Jordan")
  //๋ถ€๋ชจ๊ฐ€ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ
  basket.jump(50)

ํด๋ž˜์Šค์™€ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‹ค๋ฅธ์ 

 

์œ„์—์„œ ์‚ดํŽด๋ณด๋ฉด ํด๋ž˜์Šค๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋‚˜ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋‚˜ ๋˜‘๊ฐ™์€ ๊ฒƒ์ฒ˜๋Ÿผ ๋Š๊ปด์ง„๋‹ค.

 

ํ•˜์ง€๋งŒ ํด๋ž˜์Šค๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅธ ์ ์ด ๋ช‡ ๊ฐ€์ง€ ์žˆ๋‹ค.

 

1. ํด๋ž˜์Šค๋Š” ํ˜ธ์ถœํ•  ์ˆ˜ ์—†๋‹ค.

 

ํด๋ž˜์Šค๋กœ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

function Person {...}
Person() //OK

class User {...{
User() //ERROR

 

2. ์ถœ๋ ฅ ์‹œ ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ถœ๋ ฅ๋จ

 

console.log(User) //[class User]
console.log(Person) //[Function: Person]

 

3. ํด๋ž˜์Šค ์•ˆ์˜ ๋ฉ”์„œ๋“œ๋Š” ์—ด๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค.

 

์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ๋งŒ๋“  ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์—ด๊ฑฐํ•˜๋ฉด ๋ฉ”์„œ๋“œ๊นŒ์ง€ ์—ด๊ฑฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํด๋ž˜์Šค๋Š” ๊ทธ๋ ‡์ง€ ์•Š๋‹ค.

 

๊ทธ ์ด์œ ๋Š” ํด๋ž˜์Šค์˜ enumerable ํ”„๋กœํผํ‹ฐ ํ”Œ๋ž˜๊ทธ๊ฐ€ false๋กœ ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

for (let i in user) {
    console.log(i) 
}
    /*
    uid
    name
    age
    */
    
for (let i in person) {
    console.log(i) 
}
    /*
    uid
    name
    age
    getInfo
    */

ํด๋ž˜์Šค ํ•„๋“œ ๋ฐ”์ธ๋”ฉ๋œ ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ

 

setTimeout ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋กœ ๋งŒ๋“ค๋ฉด this๊ฐ€ ์‚ฌ๋ผ์ ธ๋ฒ„๋ ค์„œ undefined๊ฐ€ ๋œ๋‹ค.
 
 
undefined๋˜๋Š” ์ด์œ ๋Š” ์ปจํ…์ŠคํŠธ๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ setTimeout์€ this์— window๋ฅผ ํ• ๋‹นํ•ด์„œ window๊ฐ์ฒด์—” ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ž˜ํผ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ด์ค€๋‹ค. (ํ•จ์ˆ˜ ๋ฐ”์ธ๋”ฉ)
 
class Button {
    constructor(value) {
      this.value = value;
    }
  
    click() {
      console.log(this.value);
    }
  }
  
  let button = new Button("์•ˆ๋…•ํ•˜์„ธ์š”.");
  setTimeout(button.click, 1000); // undefined

//์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ž˜ํ•‘์„ ํ•ด์ค˜ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.
class AnotherButton {
    constructor(value) {
      this.value = value;
    }

    click = () => {
      console.log(this.value);
    }
  }

  let button1 = new AnotherButton("ํ•˜์ด")
  setTimeout(button1.click,1000) //ํ•˜์ด

๋™์ ์œผ๋กœ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

 

ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ๋™์ ์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

function makePerson() {
    return class {
      walk() {
        console.log("walk")
      }
    }
  }

 

ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์†๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

class P extends makePerson() {}
let p = new P()
p.walk() //walk

์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

์„œ๋ธŒํด๋ž˜์Šค ๋˜๋Š” ์ž์‹ ํด๋ž˜์Šค๊ฐ€ ์ž์‹ ์˜ ์Šˆํผํด๋ž˜์Šค๋“ค ๋˜๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค๋“ค ์ค‘ ํ•˜๋‚˜์— ์˜ํ•ด ์ด๋ฏธ ์ œ๊ณต๋œ 
๋ฉ”์†Œ๋“œ๋ฅผ ํŠน์ •ํ•œ ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ - ์œ„ํ‚ค ๋ฐฑ๊ณผ -

 

์ฆ‰, ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์ด๋ฏธ ๊ตฌํ˜„๋œ ๋ฉ”์„œ๋“œ๋ฅผ ์ž์‹ ํด๋ž˜์Šค์—์„œ ๋ฐ”๊พธ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

super.๋ฉ”์„œ๋“œ์ด๋ฆ„(ํŒŒ๋ผ๋ฏธํ„ฐ)๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ดํ›„์— ์ถ”๊ฐ€ํ•ด์ค„ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

class ValleyBallPlayer extends Player {
    spike() {
      console.log("Spike")
    }
    jump(space) {
      super.jump(space)
      this.spike()
    }
  }

 

์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ

 

๋งŒ์•ฝ ๋ถ€๋ชจ๊ฐ€ ์ƒ์„ฑ์ž๊ฐ€ ์žˆ๊ณ  ์ž์‹์˜ ์ƒ์„ฑ์ž๋ฅผ ๋‹ค๋ฅด๊ฒŒ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด super(ํŒŒ๋ผ๋ฏธํ„ฐ)๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 class FootBallPlayer extends Player {
    constructor(name,shoeSize) {
      //๋ถ€๋ชจ๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž๋Š” super๋ฅผ ์จ์ค˜์•ผํ•จ.
      super(name)
      this.shoeSize = shoeSize
    }
  }

 


Reference

 

 

๋ชจ๋˜ JavaScript ํŠœํ† ๋ฆฌ์–ผ

 

ko.javascript.info

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€