μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- error
- fe
- node.js
- μ½λ©
- λͺ¨κ°μ½
- ν¬λ‘€λ§
- μκ³ λ¦¬μ¦
- mongodb
- κ΅λΉμ§μ
- CSS
- νλ‘μ νΈ
- Til
- heapq
- νλ‘κ·Έλλ¨Έμ€
- HTML
- KDT
- κ°λ°
- λλ¦Όμ½λ©
- νλ‘ νΈμλ
- 그리λ
- ν μ΄νλ‘μ νΈ
- javascript
- λ Έλ§λμ½λ
- μ½λ©ν μ€νΈ
- react
- νμ΄μ¬
- μ½λ©μ ν
- Python
- λ°±μ€
- JS
- Today
- Total
π± → π³
[node.js] class VS μμ±μ ν¨μ λ³Έλ¬Έ
π μμ±μ ν¨μ
μμ±μ ν¨μλ μν κΈ°κ³, μμ±μλ μν..
-> 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();
'Server > Node.js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[node.js] Yarnμ΄λ? (0) | 2022.09.02 |
---|---|
[node.js] νλ μμν¬ μμ΄ CRUD ꡬνν΄λ³΄κΈ° (0) | 2022.08.27 |
[node.js] GET μμ² μ²λ¦¬ (1) | 2022.08.25 |
macμμ node.js κ°λ° νκ²½ μΈν (0) | 2022.08.24 |
[node.js] node.js μ λ¬Έ (0) | 2022.08.22 |