๐ 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: ์๋ฒ ์์ ์ฝ๋๊ฐ ๋ณ๊ฒฝ๋๊ฑฐ๋ ์ข ๋ฃ๋๋ฉด ์๋์ผ๋ก ์ฌ์คํ์์ผ์ค
- [ํฐ๋ฏธ๋] npm install --save-dev nodemon
- package.json์ ์คํฌ๋ฆฝํธ ์ถ๊ฐ
- "scripts": { "server": "nodemon app.js", "start": "nodemon --watch \"./routes/*.js\" --exec \"npm run server\"" },
- ์คํ: [ํฐ๋ฏธ๋] npm run server
'KDT > TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
8/31 TIL : File-system(JS, Promise, async/await), Routing w/o framework (0) | 2022.09.18 |
---|---|
8/29 TIL : JSDoc, ํ๋ ์์ํฌ ์์ด CRUD ๊ตฌํ, JS module (0) | 2022.09.15 |
8/24 TIL : node.js, promise, async/await (0) | 2022.09.07 |
8/5 TIL : Git branch/merge/ํ์ (0) | 2022.09.05 |
8/1 8/3 TIL : ๋ฎค์ง ํ๋ ์ด์ด (0) | 2022.09.05 |