Javascript

[JS] Class

Alexim 2022. 6. 10. 00:53

๐Ÿ“— Class

- class๋Š” ํ•จ์ˆ˜์˜ ํ•œ ์ข…๋ฅ˜์ด๋‹ค.

- class ์ •์˜๋Š” ํ•ญ์ƒ "strict mode"๋‹ค. 

- class ์„ ์–ธ์€ ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ๋‹ฌ๋ฆฌ ํ˜ธ์ด์ŠคํŒ…์˜ ๋Œ€์ƒ์ด ์•„๋‹ˆ๋‹ค.

- ์„ ์–ธํ•œ class๋Š” ์žฌ์„ ์–ธ ํ•  ์ˆ˜ ์—†๋‹ค.

- class์˜ ๋ฉ”์„œ๋“œ๋Š” ์—ด๊ฑฐํ•  ์ˆ˜ ์—†๋‹ค. : non-enumerable 

 

๐Ÿ“— constructor

- ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ํŠน๋ณ„ํ•œ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ

- new์— ์˜ํ•ด ์ž๋™์œผ๋กœ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ํŠน๋ณ„ํ•œ ์ ˆ์ฐจ ์—†์ด ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ๋‹ค.

- class ์•ˆ์— ํ•œ ๊ฐœ๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

https://ko.javascript.info/class

 

ํด๋ž˜์Šค์™€ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

 

ko.javascript.info

class User {
  constructor(name) { this.name = name; }
  sayHi() { alert(this.name); }
}

// ํด๋ž˜์Šค๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
alert(typeof User); // function

// ์ •ํ™•ํžˆ๋Š” ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
alert(User === User.prototype.constructor); // true

// ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๋Š” User.prototype์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
alert(User.prototype.sayHi); // alert(this.name);

// ํ˜„์žฌ ํ”„๋กœํ† ํƒ€์ž…์—๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๋‘ ๊ฐœ์ž…๋‹ˆ๋‹ค.
alert(Object.getOwnPropertyNames(User.prototype)); // constructor, sayHi

 

  1. User๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ๋ณธ๋ฌธ์€ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ constructor์—์„œ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋ณธ๋ฌธ์ด ๋น„์›Œ์ง„ ์ฑ„๋กœ ํ•จ์ˆ˜๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
  2. sayHi๊ฐ™์€ ํด๋ž˜์Šค ๋‚ด์—์„œ ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ User.prototype์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

new User๋ฅผ ํ˜ธ์ถœํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ , ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•จ์ˆ˜์˜ prototype ํ”„๋กœํผํ‹ฐ์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฉ”์„œ๋“œ๋ฅผ prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ์ด ๊ณผ์ •์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์—์„œ ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜์˜ prototype ํ”„๋กœํผํ‹ฐ

 

ko.javascript.info

 

 

๐Ÿ“— class ์ƒ์† - extends, super

๐Ÿ“— extends

class Animal {
  constructor(name) {
    this.speed = 0;
    this.name = name;
  }
  run(speed) {
    this.speed = speed;
    alert(`${this.name} ์€/๋Š” ์†๋„ ${this.speed}๋กœ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.`);
  }
  stop() {
    this.speed = 0;
    alert(`${this.name} ์ด/๊ฐ€ ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค.`);
  }
}

let animal = new Animal("๋™๋ฌผ");
class Rabbit extends Animal {
  hide() {
    alert(`${this.name} ์ด/๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค!`);
  }
}

let rabbit = new Rabbit("ํฐ ํ† ๋ผ");

rabbit.run(5); // ํฐ ํ† ๋ผ ์€/๋Š” ์†๋„ 5๋กœ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.
rabbit.hide(); // ํฐ ํ† ๋ผ ์ด/๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค!

- extends๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Animal ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

- Rabbit์—์„œ Animal์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๋ฎ์–ด์“ฐ๊ณ  ๋‚œ ํ›„ ๋‹ค์‹œ Animal์— ์žˆ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด super๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ“— super

- super.method๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

- ์ž์‹ ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

class Animal {

  constructor(name) {
    this.speed = 0;
    this.name = name;
  }

  run(speed) {
    this.speed = speed;
    alert(`${this.name}๊ฐ€ ์†๋„ ${this.speed}๋กœ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.`);
  }

  stop() {
    this.speed = 0;
    alert(`${this.name}๊ฐ€ ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค.`);
  }

}

class Rabbit extends Animal {
  hide() {
    alert(`${this.name}๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค!`);
  }

  stop() {
    super.stop(); // ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ stop์„ ํ˜ธ์ถœํ•ด ๋ฉˆ์ถ”๊ณ ,
    this.hide(); // ์ˆจ์Šต๋‹ˆ๋‹ค.
  }
}

let rabbit = new Rabbit("ํฐ ํ† ๋ผ");

rabbit.run(5); // ํฐ ํ† ๋ผ๊ฐ€ ์†๋„ 5๋กœ ๋‹ฌ๋ฆฝ๋‹ˆ๋‹ค.
rabbit.stop(); // ํฐ ํ† ๋ผ๊ฐ€ ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค. ํฐ ํ† ๋ผ๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค!

๐Ÿ“• ์ƒ์† ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ super๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

class Rabbit extends Animal {
  constructor(name, earLength) {
    this.speed = 0;
    this.name = name;
    this.earLength = earLength;
  }

  hide() {
    alert(`${this.name} ์ด/๊ฐ€ ์ˆจ์—ˆ์Šต๋‹ˆ๋‹ค!`);
  }
}

Rabbit์—์„œ name, earLength๋ฅผ ์ง€์ •ํ•˜๋Š” ์†์„ฑ์„ ๋„ฃ์œผ๋ฉด ์˜ค๋ฅ˜๊ฐ€๋‚œ๋‹ค. -> super๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„์„œ

 

- ์ผ๋ฐ˜ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ์ฐจ์ด๋Š” new์™€ ํ•จ๊ป˜ ๋“œ๋Ÿฌ๋‚œ๋‹ค.

1. ์ผ๋ฐ˜ ํด๋ž˜์Šค๊ฐ€ new์™€ ํ•จ๊ป˜ ์‹คํ–‰๋˜๋ฉด, ๋นˆ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  this์— ์ด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•œ๋‹ค. 

2. ์ƒ์† ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋ฉด, ์ƒ์† ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ๋นˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  this์— ์ด ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๋Š” ์ผ์„ ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธธ ๊ธฐ๋Œ€ํ•œ๋‹ค.

 

- ์ด๋Ÿฐ ์ฐจ์ด ๋•Œ๋ฌธ์— ์ƒ์† ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ๋Š” super๋ฅผ ํ˜ธ์ถœํ•ด ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ์‹คํ–‰ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด this๊ฐ€ ๋  ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

class Animal {

  constructor(name) {
    this.speed = 0;
    this.name = name;
  }

  // ...
}

class Rabbit extends Animal {

  constructor(name, earLength) {
    super(name);
    this.earLength = earLength;
  }

  // ...
}

// ์ด์ œ ์—๋Ÿฌ ์—†์ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
let rabbit = new Rabbit("ํฐ ํ† ๋ผ", 10);
alert(rabbit.name); // ํฐ ํ† ๋ผ
alert(rabbit.earLength); // 10

 

์ถœ์ฒ˜ ๋ฐ ์ฐธ๊ณ 
https://ko.javascript.info/class-inheritance
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/class
https://uiyoji-journal.tistory.com/101