🌱 → 🌳

[node.js] class VS μƒμ„±μž ν•¨μˆ˜ λ³Έλ¬Έ

Server/Node.js

[node.js] class VS μƒμ„±μž ν•¨μˆ˜

BAY 2022. 8. 26. 14:43
728x90

πŸ“Œ μƒμ„±μž ν•¨μˆ˜

μƒμ„±μž ν•¨μˆ˜λŠ” μ™€ν”Œ 기계, μƒμ„±μžλŠ” μ™€ν”Œ..

-> ES6κ°€ λ‚˜μ˜€λ©΄μ„œ classκ°€ λŒ€μ²΄ (μΌμ’…μ˜ syntactic sugar μž„)

 

πŸ“Œ class λž€?

객체지ν–₯ ν”„λ‘œκ·Έλž˜λ°(OOP)μ—μ„œ νŠΉμ • 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•΄ λ³€μˆ˜μ™€ λ©”μ†Œλ“œλ₯Ό μ •μ˜ν•˜λŠ” μΌμ’…μ˜ ν‹€ -> 객체의 섀계도

μΈμŠ€ν„΄μŠ€ν™” : 클래슀(객체의 섀계도)λ₯Ό μ΄μš©ν•΄μ„œ μ‹€μ œ 객체λ₯Ό λ§Œλ“œλŠ” κ³Όμ •

βœ”οΈ class 둜 κ΅¬ν˜„ μ˜ˆμ‹œ

// @ts-check

class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  drive() {
    console.log(`${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`);
  }
}

const hyundai = new Car('hyundai', 'darkblue');
const porsche = new Car('porsche', 'black');
const toyota = new Car('toyota', 'silver');

console.log(hyundai.brand, hyundai.color);

porsche.drive();
toyota.drive();

 

 

βœ”οΈ μƒμ„±μž ν•¨μˆ˜λ‘œ κ΅¬ν˜„

// μƒμ„±μž ν•¨μˆ˜λ‘œ μœ„μ™€ λ™μΌν•˜κ²Œ κ΅¬ν˜„
function Car(brand, color) {
  this.brand = brand;
  this.color = color;
  this.drive = function () {
    console.log(`${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`);
  };
}

const hyundai = new Car('hyundai', 'darkblue');
const porsche = new Car('porsche', 'black');
const toyota = new Car('toyota', 'silver');

console.log(hyundai.brand, hyundai.color);

porsche.drive();
toyota.drive();

 

βœ… class vs μƒμ„±μž ν•¨μˆ˜

 

1. classλŠ” μ•”λ¬΅μ μœΌλ‘œ strict mode κ°€ 기본적으둜 μ‹€ν–‰ 됨

(var 와 let, const 의 관계λ₯Ό λ– μ˜¬λ¦¬μ‹œλ©΄ νŽΈν•¨)

 

2. 상속이 더 편리

  • μƒμ„±μž ν•¨μˆ˜ : ν”„λ‘œν†  νƒ€μž… 체인으둜 상속
  • 클래슀 : extends ν‚€μ›Œλ“œλ‘œ 상속

3. 정적 λ©”μ„œλ“œ μ‚¬μš© 방식

  • μƒμ„±μž ν•¨μˆ˜ : ν•¨μˆ˜ λͺΈμ²΄μ— 직접 μΆ”κ°€
  • 클래슀 : Static ν‚€μ›Œλ“œ μ‚¬μš©

 

βœ”οΈ class의 strict mode

μ•„λž˜μ˜ μ½”λ“œλ₯Ό μ‹€ν–‰ν•˜λ©΄, classλŠ” error λ°œμƒ/ μƒμ„±μž ν•¨μˆ˜λŠ” κ²°κ³Ό 좜λ ₯

// @ts-check

const hyundai = new Car('hyundai', 'darkblue');
const porsche = new Car('porsche', 'black');
const toyota = new Car('toyota', 'silver');

console.log(hyundai.brand, hyundai.color);

porsche.drive();
toyota.drive();

class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  drive() {
    console.log(`${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`);
  }
}

μƒμ„±μž ν•¨μˆ˜λŠ” strict mode κ°€ μ μš©λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ—, μ„ μ–Έμ „에 μ‚¬μš©μ΄ λ˜μ–΄λ„ μžλ™μœΌλ‘œ ν˜Έμ΄μŠ€νŒ…(hoisting)이 μΌμ–΄λ‚˜μ„œ λ¬Έμ œκ°€ μ—†μŒ 

 

βœ”οΈ class의 μƒμ† 

class : 기쑴의 ν΄λž˜μ“°μ˜ 틀을 ν† λŒ€λ‘œ μƒˆλ‘œμš΄ 클래슀λ₯Ό λ§Œλ“€ 수 있음

κΈ°μ‘΄ 클래슀의 λ³€μˆ˜, λ©”μ†Œλ“œ 등을 κ·ΈλŒ€λ‘œ κ°€μ Έκ°€λ©΄μ„œ μžμ‹ μ΄ μ›ν•˜λŠ” λ³€μˆ˜, λ©”μ†Œλ“œλ₯Ό μΆ”κ°€ λ˜λŠ” λ³€κ²½ν•΄μ„œ μ‚¬μš© κ°€λŠ₯

상속은 extendsλ₯Ό μ΄μš©ν•΄μ„œ μ‰½κ²Œ κ°€λŠ₯ 

λΆ€λͺ¨ μš”μ†Œμ˜ 것을 κ·ΈλŒ€λ‘œ λ°›μ•„ 올 λ•ŒλŠ” super μ‚¬μš© 

// @ts-check

class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  drive() {
    console.log(`${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`);
  }
}

// js ν•œ νŒŒμΌλ‹Ή ν•˜λ‚˜μ˜ ν΄λž˜μ“° ꢌμž₯ν•΄μ„œ eslint λ‚œλ¦¬λ‚¨ γ…‹γ…‹
class ElecCar extends Car {
  constructor(brand, color, fuel) {
    super(brand, color);
    this.fuel = fuel;
  }

  showFuel() {
    console.log(`ν•΄λ‹Ή μžλ™μ°¨λŠ” ${this.fuel}의 힘으둜 μ£Όν–‰ν•©λ‹ˆλ‹€.`);
  }
}

const hyundai = new Car('hyundai', 'darkblue');
const porsche = new Car('porsche', 'black');
const toyota = new Car('toyota', 'silver');
const tesla = new ElecCar('tesla', 'red', 'electricity');

// console.log(hyundai.brand, hyundai.color);
// porsche.drive();
// toyota.drive();
console.log(tesla.brand, tesla.color, tesla.fuel);
tesla.showFuel();

μœ„μ˜ μ½”λ“œ κ·Έλ¦Ό μ„€λͺ…

 

πŸ“ overriding : λΆ€λͺ¨ 클래슀의 λ©”μ†Œλ“œλ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜κ³  싢지 μ•Šμ„ λ•Œ μƒμ† 받은 ν΄λž˜μŠ€μ—μ„œ λ©”μ†Œλ“œλ₯Ό μž„μ˜λ‘œ λ³€κ²½ν•˜μ—¬ μ‚¬μš©ν•˜λŠ” 것

class ElecCar extends Car {
  constructor(brand, color, fuel) {
    super(brand, color);
    this.fuel = fuel;
  }

  drive() {
    console.log(
      `${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ ${this.fuel}의 힘으둜 μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`
    );
  }
}

 

πŸ“ super

: λΆ€λͺ¨ ν΄λž˜μŠ€μ— μ ‘κ·Όν•˜κ³  싢을 λ•Œ μ‚¬μš© 

λΆ€λͺ¨ λ©”μ„œλ“œμ— μ ‘κ·Ό 

super.method()

λΆ€λͺ¨ μƒμ„±μžμ— μ ‘κ·Ό

super(constructor)

 

πŸ“ instanceof

: νŠΉμ • objectκ°€ ν•΄λ‹Ή 클래슀λ₯Ό 톡해 λ§Œλ“€μ–΄μ‘ŒλŠ”μ§€ μ—¬λΆ€λ₯Ό μ•Œμ•„λ³΄λŠ” λͺ…λ Ήμ–΄ 

const hyundai = new Car('hyundai', 'white');
const tesla = new ElecCar('tesla', 'red', 'electricity');
console.log(hyundai instanceof Car); //true
console.log(tesla instanceof Car); //true (상속 λ°›μ•„μ™”μœΌλ‹ˆκΉŒ)

 

βœ”οΈ μƒμ„±μž ν•¨μˆ˜μ˜ 상속

μƒμ„±μž ν•¨μˆ˜μ—μ„œ 상속을 κ΅¬ν˜„ν•˜λ €λ©΄ prototype μ΄λΌλŠ” 객체λ₯Ό μ‚¬μš©

(class μ“°μ‚Ό)

function Car(brand, color) {
  this.brand = brand;
  this.color = color;
  this.drive = function () {
    console.log(`${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`);
  };
}

function ElecCar(brand, color, fuel) {
  Car.call(this, brand, color);
  this.fuel = fuel;

  this.drive = function () {
    console.log(
      `${this.brand}의 ${this.color}색 μžλ™μ°¨κ°€ ${this.fuel}의 힘으둜 μ£Όν–‰ μ€‘μž…λ‹ˆλ‹€.`
    );
  };
}

ElecCar.prototype = Object.create(Car.prototype);
ElecCar.prototype.constructor = ElecCar;

const tesla = new ElecCar('tesla', 'red', 'elec');
tesla.drive();

 

728x90