js

javascript ํด๋ž˜์Šค(class)

์œถโ‰ 2022. 11. 1. 15:50

* class: ES6์— ์ถ”๊ฐ€๋œ ์ŠคํŽ™. class๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‚ด๋ถ€์— constructor๊ฐ€ ์žˆ์Œ. new๋ฅผ ํ†ตํ•ด์„œ ํ˜ธ์ถœํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰๋จ.

- class์— ์žˆ๋Š” constructor ๋ถ€๋ถ„์€ ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ’์œผ๋กœ ์ธ์ˆ˜๋ฅผ ๋„˜๊ฒจ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” name๊ณผ age๊ฐ€ ๋งŒ๋“ค์–ด์ง

- showName์ฒ˜๋Ÿผ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๋Š” User2์˜ ํ”„๋กœํ† ํƒ€์ž…์„ ์ž˜๋ผ๋ƒ„.

const User = function(name,age){
  this.name = name;
  this.age = age;
  this.showName = function () {
    console.log(this.name);
  };
};
const mike = new User("Mike",30);

class User2 {
  constructor(name,age){
    this.name = name;
    this.age = age;
  }
  showName(){
    console.log(this.name);
  }
}
const tom = new User2("Tom",19);

=> ๊ทธ๋ž˜์„œ mike๋Š” ๊ฐ์ฒด ๋‚ด๋ถ€์— showName์ด ์žˆ๊ณ ,

Tom์€ ํ”„๋กœํ† ํƒ€์ž… ๋‚ด๋ถ€์— showName์ด ์žˆ์Œ

์‚ฌ์šฉ๋ฒ•์€ mike.showName(); , tom.showName(); ๋™์ผ

-  class๋กœ ๋งŒ๋“  class User2์—์„œ new๋ฅผ ๋นผ๊ณ  ์‹คํ–‰ํ•  ๊ฒฝ์šฐ console์ฐฝ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ์„ค๊ณ„๋˜์–ด ์žˆ์Œ.

 

 

 

 

- ์ƒ์„ฑ์žํ•จ์ˆ˜์—์„œ ์“ด ๋ถ€๋ถ„์„ class๋กœ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

const User = function(name,age){
  this.name = name;
  this.age = age;
  // this.showName = function () {
  //   console.log(this.name);
  // };
};
User.prototype.showName = function(){
  console.log(this.name);
}
const mike = new User("Mike",30);

- new๋ฅผ ๋นผ๊ณ  ์‹คํ–‰ํ•  ๊ฒฝ์šฐ undefined๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ Userํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์—์„œ return๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ ์•„๋ฌด๊ฒƒ๋„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ. ( const mike = User("Mike",30)์—์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์ด undefined๊ณ  ๊ทธ ๊ฐ’์ด mike๋กœ ๋“ค์–ด๊ฐ)

 

- for in ๋ฌธ ์‚ฌ์šฉํ•ด ์ˆœํšŒํ•  ๊ฒฝ์šฐ mike๋Š” name, age, showName ๋‹ค ๋‚˜์˜ค์ง€๋งŒ tom์˜ ๊ฒฝ์šฐ์—๋Š” name๊ณผ age๋งŒ ๋‚˜์˜ด

<- for in๋ฌธ์€ ํ”„๋กœํ† ํƒ€์ž…์— ํฌํ•จ๋œ ํ”„๋กœํผํ‹ฐ๋“ค์„ ๋‹ค ๋ณด์—ฌ์ฃผ๊ณ  ๊ฐ์ฒด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ํŒ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ hasOwnproperty๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๋Š”๋ฐ class ๋ฉ”์„œ๋“œ๋Š” for in๋ฌธ์—์„œ ์ œ์™ธ๋จ

 

 

 

 

* ์ƒ์„ฑ์žํ•จ์ˆ˜๋Š” ํ”„๋กœํ† ํƒ€์ž…์„ ์ด์šฉํ•ด ์ƒ์†์„ ๊ตฌํ˜„ํ–ˆ์—ˆ๋Š”๋ฐ class์—์„œ์˜ ์ƒ์†์€  extends ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

- car class์—์„œ ์„ ์–ธํ•œ color์™€ wheels๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๊ณ  ํ”„๋กœํ† ํƒ€์ž…์—์„œ๋Š” park๊ฐ€ ์žˆ์Œ.

- ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•œ ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํ† ํƒ€์ž… ๋ฐ‘์œผ๋กœ ๋“ค์–ด๊ฐ€๋Š”๋ฐ ์—ฌ๊ธฐ์— drive์™€ stop์ด ์žˆ์Œ.

class car{
  constructor(color){
    this.color = color;
    this.wheels = 4;
  }
  drive(){
    console.log("drive..");
  }
  stop(){
    console.log("stop!");
  }
}
//car๋ฅผ ์ƒ์†ํ•ด์„œ bmw ๋งŒ๋“ค๊ธฐ
class Bmw extends car{
  park(){
    console.log("park");
  }
}
const z4 = new Bmw("blue");

* Class - ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ(method overriding)

- ๋™์ผํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌํ•  ๊ฒฝ์šฐ ๋ฎ์–ด์“ฐ๊ธฐ๊ฐ€ ๋จ.

- ๋งŒ์•ฝ ๊ธฐ์กด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ super๋ฅผ ์‚ฌ์šฉ. -> stop๊ณผ no๊ฐ€ ๊ฐ™์ด ์ถœ๋ ฅ

class Car{
  constructor(color){
    this.color = color;
    this.wheels = 4;
  }
  drive(){
    console.log("drive..");
  }
  stop(){
    console.log("stop!");
  }
}

class Bmw extends Car{
  park(){
    console.log("park");
  }
  stop(){
    super.stop();
    console.log("NO");
  }
}
const z4 = new Bmw("blue");

=> ์ด๋ ‡๊ฒŒ super.๋ฉ”์†Œ๋“œ ์ด์šฉํ•ด์„œ ๋ถ€๋ชจ class์— ์ •์˜๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.

 

*Class - ์˜ค๋ฒ„๋ผ์ด๋”ฉ(overriding) (์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ)

- constructor์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— super constructor ์ฆ‰ ๋ถ€๋ชจ ์ƒ์„ฑ์ž๋ฅผ ๋ฐ˜๋“œ์‹œ ํ˜ธ์ถœํ•ด์•ผ ํ•จ

- class์˜ constructor๋Š” ๋นˆ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ง€๊ณ  this๋กœ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋จ. ๋ฐ˜๋ฉด์— extends ์จ์„œ ๋งŒ๋“  ์ž์‹ ํด๋ž˜์Šค๋Š” ๋นˆ ๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ  this์— ํ• ๋‹นํ•˜๋Š” ์ž‘์—…์„ ๊ฑด๋„ˆ๋›ฐ๊ธฐ ๋•Œ๋ฌธ์— ํ•ญ์ƒ super();๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถ€๋ชจํด๋ž˜์Šค์˜ constructor๋ฅผ ์‹คํ–‰ํ•ด์ค˜์•ผ ํ•จ.

class Car{
  constructor(color){
    this.color = color;
    this.wheels = 4;
  }
  drive(){
    console.log("drive..");
  }
  stop(){
    console.log("stop!");
  }
}

class Bmw extends Car{
  constructor(){
    super();
    this.navigation = 1;
  }
  park(){
    console.log("park");
  }
}
// const z4 = new Bmw("blue"); -> undefined

- super(); ์“ฐ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ

class Car{
  constructor(color){
    this.color = color;
    this.wheels = 4;
  }
  drive(){
    console.log("drive..");
  }
  stop(){
    console.log("stop!");
  }
}

class Bmw extends Car{
  constructor(){
    this.navigation = 1;
  }
  park(){
    console.log("park");
  }
}
// const z4 = new Bmw("blue"); -> ์—๋Ÿฌ๋ฐœ์ƒ

- ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž์‹ ํด๋ž˜์Šค์˜ constructor์— ๋™์ผํ•œ ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•จ

constructor๊ฐ€ ์—†์œผ๋ฉด ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ–‰๋™ํ•ด์„œ ์ž์‹์ƒ์„ฑ์ž๋Š” ๋ถ€๋ชจ ์ƒ์„ฑ์ž์— ์žˆ๋Š” ๊ฒƒ์„ ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœํ•ด์•ผํ•จ

์ž์‹์ƒ์„ฑ์ž์— constructor๊ฐ€ ์žˆ์œผ๋ฉด super๋ฅผ ์ด์šฉํ•ด ํ˜ธ์ถœํ•ด์ฃผ๊ณ  this.ํ”„๋กœํผํ‹ฐ๋กœ ํ• ๋‹นํ•ด์ค˜์•ผ ํ•จ

class Car{
  constructor(color){
    this.color = color;
    this.wheels = 4;
  }
  drive(){
    console.log("drive..");
  }
  stop(){
    console.log("stop!");
  }
}

class Bmw extends Car{
  constructor(color){
    super(color);
    this.navigation = 1;
  }
  park(){
    console.log("park");
  }
}
// const z4 = new Bmw("blue"); -> z4 / Bmw {color: 'blue', wheels: 4, navigation: 1}

 

 

'js' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

javascript async, await  (0) 2022.11.01
javascript ํ”„๋กœ๋ฏธ์Šค (Promise)  (0) 2022.11.01
javascript ์ƒ์†, ํ”„๋กœํ† ํƒ€์ž…(Prototype)  (0) 2022.10.31
javascript ๋ฉ”์„œ๋“œ call, apply, bind  (0) 2022.10.31
javascript setTimeout / setInterval  (0) 2022.10.31