KDT/TIL

8/26 TIL : class, CRUD

ebulsok 2022. 9. 14. 14:37

๐Ÿ”Ž class: ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํŠน์ • ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ์ผ์ข…์˜ ํ‹€

  • ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ ํŽธํ•˜๊ธฐ ์“ฐ๊ธฐ ์œ„ํ•œ ์ผ์ข…์˜ Syntactic sugar
  • ์›๋ž˜๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜๋กœ ์“ฐ๋˜ ๊ฒƒ์„ ES6(2015๋…„)์— ๋“ค์–ด class๋ผ๋Š” ๊ฒƒ์œผ๋กœ ์ถ”๊ฐ€
  • strict mode๊ฐ€ ์ ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„ ์–ธ ์ „์— ์‚ฌ์šฉํ•˜๋ฉด error

 

๐Ÿšฉ ์ธ์Šคํ„ด์Šคํ™”: ํด๋ž˜์Šค(๊ฐ์ฒด์˜ ์„ค๊ณ„๋„)๋ฅผ ์ด์šฉํ•ด์„œ ์‹ค์ œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •

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

  drive() {
    console.log(`${this.brand}์˜ ${this.color}์ƒ‰ ์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰์ค‘์ž…๋‹ˆ๋‹ค.`);
  }

  showSpec() {
    console.log(
      `์ด ์ฐจ์˜ ๋ธŒ๋žœ๋“œ๋Š” ${this.brand}์ด๊ณ  ์ƒ‰์ƒ์€ ${this.color}์ž…๋‹ˆ๋‹ค.`
    );
  }
}

const hyundai = new Car('Hyundai', 'blue');
const porsche = new Car('Porsche', 'black');
const toyota = new Car('Toyata', '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}์ƒ‰ ์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰์ค‘์ž…๋‹ˆ๋‹ค.`);
  };
}

 

๐Ÿšฉ class์™€ ์ƒ์„ฑ์žํ•จ์ˆ˜์˜ ์ฐจ์ด

  • ํด๋ž˜์Šค๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ strict mode๊ฐ€ ์‹คํ–‰๋œ๋‹ค(ํ•จ์ˆ˜ ์„ ์–ธ์€ ์ž๋™ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค)
  • ์ƒ์†์ด ํŽธ๋ฆฌํ•˜๋‹ค
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜: ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์œผ๋กœ ์ƒ์†
    • ํด๋ž˜์Šค: extend ํ‚ค์›Œ๋“œ๋กœ ์ƒ์†
  • ์ •์  ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ๋ฐฉ์‹
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜: ํ•จ์ˆ˜ ๋ชธ์ฒด์— ์ง์ ‘ ์ถ”๊ฐ€
    • ํด๋ž˜์Šค: Static ํ‚ค์›Œ๋“œ ์ ์šฉ

 

๐Ÿšฉ ์ƒ์†: ๊ธฐ์กด ํด๋ž˜์Šค์˜ ํ‹€์„ ํ† ๋Œ€๋กœ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

  • ๊ธฐ์กด์˜ ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ ๋“ฑ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ๊ฐ€๋ฉด์„œ ์›ํ•˜๋Š” ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ๋ฅผ ์ถ”๊ฐ€/๋ณ€๊ฒฝํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • class ์ƒ์†ํ• ํด๋ž˜์Šค์ด๋ฆ„ extends ์ƒ์†๋ฐ›์„ํด๋ž˜์Šค์ด๋ฆ„
  • ๋ถ€๋ชจ ์š”์†Œ์˜ ๊ฒƒ์„ ๊ทธ๋Œ€๋กœ ๋ฐ›์•„ ์˜ฌ ๊ฒฝ์šฐ์—๋Š” super ์‚ฌ์šฉ
    • ๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ์— ์ ‘๊ทผ: super.method()
    • ๋ถ€๋ชจ ์ƒ์„ฑ์ž์— ์ ‘๊ทผ: super(constructor)
  • over-riding: ์ƒ์† ๋ฐ›์€ ํด๋ž˜์Šค์—์„œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ
class Car {
  constructor(brand, color) {
    this.brand = brand;
    this.color = color;
  }

  drive() {
    console.log(`${this.brand}์˜ ${this.color}์ƒ‰ ์ž๋™์ฐจ๊ฐ€ ์ฃผํ–‰์ค‘์ž…๋‹ˆ๋‹ค.`);
  }

  showSpec() {
    console.log(
      `์ด ์ฐจ์˜ ๋ธŒ๋žœ๋“œ๋Š” ${this.brand}์ด๊ณ  ์ƒ‰์ƒ์€ ${this.color}์ž…๋‹ˆ๋‹ค.`
    );
  }
}

// ์ƒ์†
class ElecCar extends Car {
  constructor(brand, color, fuel) {
    super(brand, color);
    this.fuel = fuel;
  }

  drive() {
    console.log(
      `${this.brand}์˜ ${this.color}์ƒ‰ ์ž๋™์ฐจ๊ฐ€ ${this.fuel}์˜ ํž˜์œผ๋กœ ์ฃผํ–‰ํ•ฉ๋‹ˆ๋‹ค.`
    );
  }

  showFuel() {
    console.log(`ํ•ด๋‹น ์ž๋™์ฐจ๋Š” ${this.fuel}์˜ ํž˜์œผ๋กœ ์ฃผํ–‰ํ•ฉ๋‹ˆ๋‹ค.`);
  }

  // super
  showSpec() {
    super.showSpec();
    console.log(`๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์ž๋™์ฐจ๋Š” ${this.fuel}์˜ ํž˜์œผ๋กœ ์ฃผํ–‰ํ•ฉ๋‹ˆ๋‹ค.`);
  }
}
const tesla = new ElecCar('Tesla', 'red', 'electricity');
console.log(tesla.brand, tesla.color);
tesla.showFuel();

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ƒ์† ๊ตฌํ˜„(prototype ์ด๋ผ๋Š” ๊ฐ์ฒด ์‚ฌ์šฉ)
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(
      `${brand}์˜ ${color}์ƒ‰ ์ž๋™์ฐจ๊ฐ€ ${this.fuel}์˜ ํž˜์œผ๋กœ ์ฃผํ–‰ํ•ฉ๋‹ˆ๋‹ค.`
    );
  };
}
ElecCar.prototype = Object.create(Car.prototype);
ElecCar.prototype.constructor = ElecCar;
const tesla = new ElecCar('Tesla', 'white', 'electricity');
tesla.drive();

 

๐Ÿšฉ instance of: ํŠน์ • object๊ฐ€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์กŒ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์•Œ์•„๋ณด๋Š” ๋ช…๋ น์–ด

console.log(hyundai instanceof Car);
console.log(tesla instanceof Car); // ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— true
console.log(tesla instanceof Object); // ์ƒ์œ„์— Object-prototype-..์ด๊ธฐ ๋•Œ๋ฌธ์— true

๐Ÿ”Ž Back-end CRUD

  • Create -> POST
  • Read -> GET
  • Update -> PUT
  • Delete -> DELETE

 

๐Ÿšฉ req, res

req(uire): ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋“ค์–ด์˜จ ์š”์ฒญ

res(ponse): ์š”์ฒญ์— ๋”ฐ๋ฅธ ์„œ๋ฒ„์˜ ์‘๋‹ต

 

๐Ÿšฉ HTTP statusCodes

 

๐Ÿšฉ nodemon: ์„œ๋ฒ„ ์ƒ์˜ ์ฝ”๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ฑฐ๋‚˜ ์ข…๋ฃŒ๋˜๋ฉด ์ž๋™์œผ๋กœ ์žฌ์‹คํ–‰์‹œ์ผœ์คŒ

  1. [ํ„ฐ๋ฏธ๋„] npm install --save-dev nodemon
  2. package.json์— ์Šคํฌ๋ฆฝํŠธ ์ถ”๊ฐ€
  3. "scripts": { "server": "nodemon app.js", "start": "nodemon --watch \"./routes/*.js\" --exec \"npm run server\"" },
  4. ์‹คํ–‰: [ํ„ฐ๋ฏธ๋„] npm run server