OOP & OOP in JS
ch1. ๊ฐ์ฒด
๊ฐ์ฒด?
๋ชจ๋ ๊ฒ์ด ๊ฐ์ฒด๋ค!?
์ด๋ ํ ์ฌ๋ฌผ === Object.
ํ์ค์ธ๊ณ๋ฅผ ์ธ์งํ๋ฉด์ ํ๋ก๊ทธ๋๋ฐ์ ํ์!์์ ์ถ๋ฐ
โ ์์ํ๊ณ ,๋ณผ ์ ์๊ณ ,๋ง์ง ์ ์๋ ๊ฒ๋ค์ ๋ชจ๋ ๊ฐ์ฒด์ด๋ค.
๊ฐ์ฒด์ ๊ตฌ์ฑ
๋ชจ๋ ๊ฐ์ฒด๋ ์์ฑ(property)๊ณผ ํ์(method)๋ฅผ ๊ฐ์ง ์ ์์.
-
์์ฑ : ์ฌ๋์ ํค,๋์ด,๋ชธ๋ฌด๊ฒ
-
ํ์ : ์์ง์ด๋ค, ๋จน๋ค, ์๋ค, ๊ณต๋ถํ๋ค
people.eat();
people.getAddress(); //method~~~~
people.address; //property
์ ๊ณ์์ ํ์ค์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ฉ์ด
-
์์ฑ,ํ๋,property๋ ๊ฐ์๋ง.
-
ํจ์,๋ฉ์๋๋ ๊ฐ์๋ง.
ํด๋์ค์ ๊ฐ์ฒด

https://www.miltonmarketing.com/wp-content/uploads/2018/04/oopconceptimage15345345.png
class
์ด๋ ํ ๊ฐ์ฒด๋ฅผ ํํํ๊ธฐ ์ํด, ์ผ๋ฐํ๋ ํํ๋ก ๋ง๋ ๊ฒ.
์ด๋ ํย ์งํฉ์ด๋ ๋ถ๋ฅ์ ๊ฐ๊น๋ค.(์ฌ๋, ๋๋ฌผ, ์๋์ฐจ ๋ฑ)
์ถ์์ .
object(instance)
class๋ฅผ ํตํด์ ์ป์ ์ ์๋ ๊ฐ๋ณ๊ฐ์ฒด.
์ ์ผํ ์ฌ๋ฌผ.
์ค์ฒด์ ๊ฐ๊น๋ค(crong, ์ฌ์, ๋ฒค์ธ )
๊ตฌ์ฒด์ .
_# OOP๊ฐ ์์๋ค๋ฉด? ๋น์ทํ ํํ์ ์๋์ฐจ๋ฅผ ๋ง๋๋ ์ฝ๋๊ฐ ๋ฐ๋ณต์ ์ผ๋ก ํํ๋์๊ฒ์. ๊ฐ์ฒด์งํฅ์ ํตํด์ ์ด๋ฅผ ๊ฐ๋จํ๊ณ ํจ๊ณผ_์ ์ผ๋ก ํํ.
๊ฐ์ฒด์งํฅ ๊ด๋ จ ํ์ต๊ด๊ณ๋

OOP ํต์ฌ ๊ฐ๋
๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ๊ฐ์กฐ๋ ๊ฐ๋ ๋ค.
-
Class
-
์บก์ํ(Encapsulation)
-
์์(Inheritance)
-
๋คํ์ฑ(polymorphism)
-
์ถ์ํ(abstraction)
OOP ํต์ฌ ๊ฐ๋ & JavaScript
1. Class
JavaScript๋ class ์์ด ํ๋ก๊ทธ๋๋ฐ์ด ๊ฐ๋ฅํ๋ค.
ES Classes๋ ECMAScript 2015์์๋ ํ์ค์ผ๋ก ๋ฑ์ฅ. ๊ทธ์ ์๋ TypeScript ์ ๋์์ผ๋ก ์ฌ์ฉ.
์์ฑ์ ํจ์์ prototype ์์ฑ์ ํตํด์ ํด๋์ค ํํ๋ฅผ ๊ตฌ์ฑํ ์ ์์.
2. ์บก์ํ(Encapsulation)
๋ด๋ถ์์๋ง ์๊ณ ์์ผ๋ฉด ๋ ๊ฒ์, ์ธ๋ถ์ ๊ณต๊ฐํ์ง ์๋๋ค. ํ์ํ ๊ฒ์ ์ธ๋ถ์ ์ ๊ทผ์ ํ์ฉํ๋ค.
private, public ๊ฐ๋
.
JavaScript๋ ์ด๋ฅผ ์ง์ํ๊ณ ์์.
module pattern์ ํตํด์ ์ด๋ฅผ ๊ตฌํํ ์๋ ์๊ธดํจ.
3. ์์(Inheritance)
๊ณตํต๋ถ๋ถ์ ๋ถ๋ฆฌํด์ ์ฌ์ฌ์ฉํ๋ ค๋ ๋ชฉ์ .
์์ํด๋์ค์ ํ์ํด๋์ค๊ฐ์ ์ฐ๊ฒฐ์ ์ง๋๋ค.
-
๋๋โ์ฌ๋, BMW โ ์๋์ฐจ, ํฌํฌ๋ ์ธโ์ค์ฅ๋น,ย ์๋ค โ ์๋น (X)
-
kind of ๊ด๊ณ.
prototype๊ธฐ๋ฐ์ ๊ฐ์ฒด๊ฐ ์ฐ๊ฒฐ chain์ ํตํด ์์๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์์.
child.prototype = Object.create(parent.prototype);
ES6 Class์ extends ํค์๋ ์ญ์ ๋ด๋ถ์ ์ผ๋ก๋ ๊ฒฐ๊ตญ prototype์ ํตํ ์์๊ตฌ์กฐ์.
4. ๋คํ์ฑ(polymorphism)
๋ค์ํ ์ฑ์ง๋ก ๋์ํ๋ ๊ฒ.
overriding์ ํตํ ์ฌ์ ์, overloading์ ํตํ ๋ฉ์๋ ์ค๋ณต์ ์.
JavaScript๋ prototype chain๋ด์ ๋์ผํ ๋ฉ์๋๋ฅผ ๋๊ณ overriding๊ตฌํ์ ๊ฐ๋ฅ.
ํ์ง๋ง, interface๊ฐ ์๊ณ , method overloading์ ๊ณต์์ง์ํ์ง ์์
(๋ค๋ง,์ด๋ ํ ํ์ ์ ์ฒดํฌํด์, ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ํธ์ถํด์ ์คํํ๋๋ก ๊ตฌํํ ์๋ ์์)
JavaScript์ ๋คํ์ฑ์ด ์กด์ฌํ๋?๋ ์ค์ํ ๋ ผ์๊ฑฐ๋ฆฌ๋ ์๋.
์ค๋ฒ๋ก๋ฉ์ ๊ฐ์ ํด๋์ค ๋ด์์ ๋ฉ์๋ ์ด๋ฆ์ ๊ฐ์ง๋ง ๋งค๊ฐ๋ณ์๊ฐ ๋ค๋ฅธ ์ฌ๋ฌ ๋ฉ์๋๋ฅผ ์ ์ํ๋ ๊ฒ์ด๊ณ ,
์ค๋ฒ๋ผ์ด๋ฉ์ ์์ ๊ด๊ณ์ ์๋ ํด๋์ค์์ ์์ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํ์ ํด๋์ค๊ฐ ์ฌ์ ์ํ๋ ๊ฒ.
5. ์ถ์ํ(abstraction)
ํ์ํ ๋ถ๋ถ๋ง์ ์ ํํด์ ํด๋์ค๋ฅผ ๋ง๋๋ ๊ฒ.
โ์ฐจโ๋ โ๋ฒ์คโํด๋์ค์ โ์ค์ฅ๋นโํด๋์ค๋ก ๊ตฌ๋ถ์ง์ ์ ์๋๋ฐ, ๋ฒ์ค์ ์ค์ฅ๋น๋ ์๋ก ๋ค๋ฅธ ์์ฑ๊ณผ ํ์๊ฐ ํ์ํ๋ค. ์ด๋ ๊ฒ ํ์ํ ๋ถ๋ถ์ ์ ํํด์ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์๊ณ , ์ด๋ฅผ ์ถ์ํ๋ผ๊ณ ํจ.
JavaScript์์๋ ์ด๋ฅผ ํํํ๋๊ฑด ๋น์ฐํ ๊ฐ๋ฅ!
CH2. JS ๊ฐ์ฒด ํํ๋ฐฉ๋ฒ
ํ๋ก ํธ์๋์์๋ OOP๋ฅผ ?
1. object literal
class์์ด ๋ฐ๋ก object๋ก ํํ๊ฐ๋ฅ(singleton)
var healthObj = {
name : "๋ฌ๋ฆฌ๊ธฐ",
lastTime : "PM10:12",
showHealth : function() {
console.log(this.name + "๋, ์ค๋์ " + this.lastTime + "์ ์ด๋์ ํ์
จ๋ค์");
}
}
healthObj.showHealth();
2. Class - ES6
class๋ฅผ ์์ ์ ๊ณตํ๋ constructor๋ผ๋ ์์ฑ์๊ฐ ์ฃผ์ด์ง. class๋ฅผ ํธ์ถํ๋ฉด constructor๊ฐ ์๋ํธ์ถ ๋จ.
constructor๋ instance๋ฅผ ๋ง๋ค์ด์ ๋ฐํ. ๋ค์๋งํด ๊ฐ์ฒด(instance)๋ฅผ ์์ฑํด์ ๋ฐํ.
class Health{
constructor(name,healthTime) {
this.name = name;
this.healthTime = healthTime;
}
showHealth(){
console.log(this.name + "๋, ์ค๋์ " + this.healthTime + "์ ์ด๋์ ํ์
จ๋ค์");
}
}
const ho = new Health("crong", "12:12");
ho.showHealth();
https://caniuse.com/#feat=es6-class
3. Class - ES3
function์ new ํค์๋๋ก ํธ์ถํ๋ฉด, ๊ทธ ํจ์๋ constructor๊ฐ ๋จ.
const Health = function(name,healthTime) {
this.name = name;
this.healthTime = healthTime;
this.showHealth = function() {
console.log(this.name + "๋, ์ค๋์ " + this.healthTime + "์ ์ด๋์ ํ์
จ๋ค์");
}
}
const ho = new Health("crong", "12:12");
ho.showHealth();
4. Prototype object - ES3
function Health(name, healthTime) {
this.name = name;
this.healthTime = healthTime;
}
Health.prototype.showHealth = function() {
console.log(this.name + "๋, ์ค๋์ " + this.healthTime + "์ ์ด๋์ ํ์
จ๋ค์");
}
const ho = new Health("crong", "12:12");
ho.showHealth();
5. Object.create - ES5
Object.create๋ ์ง์ ์ ์ผ๋ก prototypeํค์๋๋ฅผ ์ฌ์ฉํ์ง ์์ง๋ง, Prototype object๋ฅผ ๋ง๋๋ ๊ฒ๊ณผ ๋์ผ.
const healthObj = {
showHealth : function() {
console.log(this.name + "๋, ์ค๋์ " + this.healthTime + "์ ์ด๋์ ํ์
จ๋ค์");
}
}
const ho = Object.create(healthObj, {
name: { value: "crong" },
healthTime: { value: "12:22" }
})
ho.showHealth();
Object.create๋ prototype๊ธฐ๋ฐ ์์์ ์ข๋ ๋งค๋๋ฝ๊ฒ ์ฌ์ฉํ๊ธฐ ์ํด ํ์ํ๋ค๊ณ ์ดํดํ ์ ์์.
Object.create๋ฅผ ์ฌ์ฉํ๋ฉด ์์๊ตฌ์กฐ๊ฐ ์ ๋ง๋ค์ด์ง. ํ์ง๋ง ES6 Classes์ extend๋ฅผ ์ฌ์ฉํด์ ์ด์ ๋ณด๋ค ์ฝ๊ฒ ํด๋์ค๊ฐ ์์ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๊ฒ ๋จ.
object literal vs. class
1. object literal
์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ ์์ฑํ ํ์๊ฐ ์์๋, ์ฆ ์ผ๋ฐํํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ๋จ์ํ object literal๋ก ๊ฐ์ฒด๋ฅผ ํํ.
2. class
ํ๋ ์ด์์ผ๋ก ๊ฐ๋ณ์ ์ธ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ ๋.