βπ» ν΄λμ€λ₯Ό μ΄μ©ν λͺ¨λν
κ°μ²΄ μ§ν₯
κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ΄λ?
- νλμ μ²μ¬μ§μ λ§λ€κ³ μ²μ¬μ§μ λ°νμΌλ‘ ν κ°μ²΄λ₯Ό λ§λλ νλ‘κ·Έλλ° ν¨ν΄
ex) μ²μ¬μ§(class) : μλμ°¨λ₯Ό λ§λλ μ€κ³λ(νΈλ€,λ°ν΄,μμ§ λ±) / κ°μ²΄(instance) : λΈλλ,μ΄λ¦,μκΉ λ±
ν΄λμ€μ μΈμ€ν΄μ€
- ν΄λμ€λ μΌμ’
μ μνμΌλ‘, κ°μ²΄λ₯Ό μμ±νκΈ° μν μμ΄λμ΄λ μ²μ¬μ§
- μΈμ€ν΄μ€λ ν΄λμ€μ μ¬λ‘(instance object)
- ν΄λμ€λ κ°μ²΄λ₯Ό λ§λ€κΈ° μν μμ±μ ν¨μλ₯Ό ν¬ν¨
| |
---|
prototype | λͺ¨λΈμ μ²μ¬μ§μ λ§λ€ λ μ°λ μν κ°μ²΄(original form) |
constructor | μΈμ€ν΄μ€κ° μ΄κΈ°νλ λ μ€ννλ μμ±μ ν¨μ |
this | ν¨μκ° μ€νλ λ, ν΄λΉ scopeλ§λ€ μμ±λλ κ³ μ ν μ€ν context(execution context) |
class Car {
constructor(brand, name, color){
this.brand = brand;
this.name = name;
this.color = color;
}
refuel() {
console.log(`${this.name}κ° μ°λ£λ₯Ό μ£Όμ
ν©λλ€`)
}
drive() {
console.log(`${this.name}κ° μ΄μ μ μμν©λλ€`)
}
}
let tesla = new Car('tesla', 'model y', 'black');
console.log(tesla)
console.log(tesla.drive())
- μμ±κ³Ό λ©μλ
μμ± : brand,name,color
λ©μλ : refuel(),drive()
μ μ°¨μ μΈμ΄λ?
- μ΄κΈ°μ νλ‘κ·Έλλ° μΈμ΄λ μΌλ°μ μΌλ‘ μ μ°¨μ μΈμ΄(C,ν¬νΈλ λ±)
- μ μ°¨μ μΈμ΄λ μμ°¨μ μΈ λͺ
λ Ήμ μ‘°ν©
ex) κ°μ²΄ μ§ν₯μ κ°λ
μ΄ μλ μ μ°¨ μΈμ΄
OOP(Object-Oriented Programming)
- OOPλ νλ‘κ·Έλ¨ μ€κ³ μ² ν μ€ νλ
- OOPμ λͺ¨λ κ²μ "κ°μ²΄"λ‘ κ·Έλ£Ήν
- OOPμ 4κ°μ§ μ£Όμ κ°λ
μ ν΅ν΄ μ»λ μ¬μ¬μ©μ±
OOP Basic Concepts
- Encapsulation(μΊ‘μν)
- λ°μ΄ν°(μμ±)μ κΈ°λ₯(λ©μλ)μ νλμ λ¨μλ‘ λ¬Άλ κ²
- μλ(hiding): λ΄λΆ λ°μ΄ν°λ λ΄λΆ ꡬνμ μ¨κΈ°λ©° μΈλΆμμ νμν λμ(λ©μλ)μ λ
ΈμΆ
- λμ¨ν κ²°ν©(Loose Coupling)μ μ 리 : ꡬν μμ μ μ©μ΄
- Inheritance(μμ)
- κΈ°λ³Έ ν΄λμ€μ νΉμ§μ νμ ν΄λμ€κ° λ¬Όλ €λ°λ κ²
- Absraction(μΆμν)
- λ΄λΆ ꡬνμ 볡μ‘νμ§λ§ μ€μ λ‘ λ
ΈμΆλλ λΆλΆμ λ¨μννκ² λ§λλ κ°λ
ex) μ νκΈ° λ΄λΆμλ λ§μ λΆνμ΄ λ€μ΄μμ§λ§ μ¬μ©μμκ²λ μνκΈ°μ λ²νΌ λ±μΌλ‘ μΈν°νμ΄μ€λ₯Ό λ¨μν νλ κ²
- Polymorphism(λ€νμ±)
- λμΌν λ©μλμ λν΄ κ°μ²΄μ νΉμ±μ λ§κ² κ°κΈ° λ€λ₯΄κ² μμ±νλ κ²μ΄ κ°λ₯
νλ‘ν νμ
(Prototype)
νλ‘ν νμ
μ΄λ?
νλ‘ν νμ
체μΈ
class Human{
constructor(name,sex,age){
this.name = name;
this.sex = sex;
this.age = age;
}
sleep(){
console.log(this.name + 'μ(λ) μκ³ μλ€')
}
eat(){
console.log(this.name + "μ(λ) λ¨Ήκ³ μλ€")
}
}
class Student extends Human{
constructor(name,sex,age,learning){
super(name,sex,age)
this.learning = 'Math'
}
learn(){
console.log(this.name +'μ '+ this.learning + 'μ λ°°μ°κ³ μλ€');
}
}