npm ๋ชจ๋ ๊ฐ์๊ฑฐ ์ฐ๊ธฐ ํธํ, ๋ฐ๋ช
ํ ๊ฐ์ฒด๋ฅผ ์์ฃผ๋ก ์ฝ๋๋ฅผ ์ ๋ฆฌํ๋ค.
๋ฌผ๊ฑด์ ๋ฐ๋ช
ํ๋ค๊ณ ์๊ฐํ๋ค.
๋ ๊ณ ๋ฅผ ์ค๋ค. ์ด๊ฑธ ์ด์ฉํด์ ๋ฌด์์ ๋ง๋ค์ง๋ ์ฐฝ์์ฑ์ ๋ฌ๋ฆฐ๊ฒ์ด๋ค.
๋๋ฌด ๊น๊นํด! ๋จํ๋ฅผ ๋ง๋ค๊ณ ์ถ์ด!
Copy var lamp = {
brightness: 0, //this๋ฅผ ๋ถ์ด์ง ์์๋ ์ ๊ทผ ๊ฐ๋ฅํ๋ค. ํจ์ํํ๊ฐ ์๋๋ผ์ 'ใ
'?
turnOn: function () {
this.brightness = 100;
},
turnOff: function () {
this.brightness = 0;
}
};
lamp.turnOn();
lamp.turnOff();
lamp.brightness = 10000000000000;
Copy function lamp() {
var brightness = 0; // ์บก์ํ ์ง์ญ๋ณ์๋ก ๋ง๋ค์๋ค.
this.turnOn = function () { // ํด๋ก์ ๋ก ๊ธฐ์ตํ๋ค.
brightness = 100;
console.log(brightness);
};
this.turnOff = function () {
brightness = 0;
}
};
var myLamp = new lamp();
myLamp.turnOn();
์บก์ํ(Encapsulation)
์ฌ์ฉ์๊ฐ ๋ด๋ถ์ ๋ณด๋ฅผ ์ ๊ทผํ์ง ๋ชปํ๋๋ก ํ๋ค.
Copy var lamp = (function(){
var brightness = 0;
return { // ํด๋ก์ ๋ก ๊ธฐ์ตํ๊ณ ์๋ค. ๊ฐ์ฒด๋ก ๋ฆฌํดํด์ค๋ค. - ํฌ์ธํธ
turnOn: function () {
brightness = 100;
},
turnOff: function () {
brightness = 0;
}
}
})(); // ์ง์ญ๋ณ์๋ก ๋ง๋ฌ
lamp.turnOn();
lamp.turnOff();
์ถ์ํ(Abstraction)
์ํํธ๋ ๋ณต์กํ ์ ์์ผ๋, ์ฌ์ฉ์๋ ๊ธฐ๊ณ์์ ์์ธ๋ด์ฉ์ ์ ํ์๋ ์๋ค. ์ฌ์ฉ์๋ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํด์ผ ํ๋ค.
factory ํจ์ - ํจ์๋ฅผ ๋ง๋๋๋ฐ ๊ฐ์ฒด๋ฅผ returnํ๊ฒ ๋ง๋ ๋ค. ์์ฑ์ํจ์์ ํด๋์ค๊ฐ ์๋๋ค.
๊ณต์ฅ์ฒ๋ผ ์ฐ์ด๋ด๋ ํจ์๋ฅผ ๋งํ๋ค.
ํ๊ด๋ฌธ์ ์๋ ๋จํ๋ฅผ ๋ง๋ค๊ณ ์ ํ๋ค. ์ด ๋จํ๋ ์ฌ์ฉ์๊ฐ ๊ฐ๋จํ๊ฒ ์ฌ์ฉํ๋ฉด ๋๋๊ฑฐ์ง, ์ด ์์ธ๋ด์ฉ์ ์ ํ์๋ ์๋ค.
Copy var lamp = (function () {
var brightness = 0;
return {
turnOn: function() {
brightness = 100;
},
turnOff: function() {
},
autoOnAndOff: function() {
brightness = 100;
setTimeout(function () {
brightness = 0;
}, 5000)
}
};
})();
lamp.autoOnAndOff();
๋จํ๊ฐ ๋ช ๊ฐ ๋ ํ์ํฉ๋๋ค.
construrctor ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฌ์ฉ์ฑ์ ๋์๋ค
Copy function Lamp () {
this.brightness = 0; // ์ฌ๊ธฐ์ brightness๊ฐ ๋
ธ์ถ์ด ๋์ง๋ง ์ฌ์ฌ์ฉ์ฑ์ ๋ ์ค์์ํ์ฌ ๋
ธ์ถ์ํด
}
Lamp.prototype.turnOn = function () {
this.brightness = 100;
};
Lamp.prototype.tyrnOff = function () {
this.brightness = 0;
};
Lamp.prototype.autoOnAndOff = function () {
var that = this;
that.brightness = 100;
setTimeout(function () {
that.brightness = 0;
}, 5000);
};
var lamp1 = new Lamp();
var lamp2 = new Lamp();
var lamp3 = new Lamp();
Factory๋ฅผ ์ฌ์ฉํ์ฌ brightness๋ฅผ ์ด์ฉํด์ brightness๋ฅผ privateํ๊ฒ ๋ง๋ฌ
Copy var lampPrototype = {
brightness: 0,
turnOn: function() {
this.brightness = 100;
},
turnOff: function() {
this.brightness = 0;
},
autoOnAndOff: function () {
this.brightness = 100;
setTimeout(() => {
this.brightness = 0;
}, 5000);
}
};
function createLamp () {
return Object.create(lampPrototype); // ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. ํฉํ ๋ฆฌ ํจ์๋ค.
}
var lamp1 = createLamp();
var lamp2 = createLamp();
var lamp3 = createLamp();
Factory ํจ์๋? ์ด๋ ํ ํจ์๋ฅผ ๋ง๋๋๋ฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๊ฒ ๋ง๋ ๋ค. ๊ทธ์น๋ง ์ด๊ฒ์ ์์ฑ์ํจ์๋ ํด๋์ค๊ฐ ์๋๋ค.
๋ง์ฝ brightness๋ฅผ ๋ณดํธํ๊ณ ์ถ๋ค๋ฉด?
Copy function createLamp () {
var brightness = 0; // ํด๋ก์ ๋ฅผ ์ด์ฉํ๊ณ , ์ง์ญ๋ณ์๋ก ๋ง๋ ๋ค.
return { // ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. ํฉํ ๋ฆฌํจ์
turnOn: function() {
brightness = 100;
},
turnOff: functi on() {
brightness = 0;
},
autoOnAndOff: function () {
brightness = 100;
setTimeout(() => {
brightness = 0;
}, 5000);
}
};
}
var lamp1 = createLamp();
var lamp2 = createLamp();
var lamp3 = createLamp();
Inheritance
SOLID Principle , Principle, KISS, GRASP
Copy function Car (owner) {
this.owner = owner;
}
Car.prototype.soldTo = function (newOwner) {
this.owner = newOwner;
};
var car = new Car('ken nim');
var car2 = new Car('hoho');
์ ๊ธฐ์ฐจ๋ฅผ ๋ง๋ค์ด๋ณด์
Copy function ElectricCar (owner) {
this.owner = owner;
this.power = 0;
}
ElectricCar.prototype.soldTo = function (newOwner) {
this.owner = newOwner;
}
ElectricCar.prototype.recharge = function (time) {
var that = this;
setTimeout(function () {
that.power = Math.min((time / 100), 100);
}, time);
};
Car์์ฑ์ ํจ์์ ์ ์ฌํ ๋ถ๋ถ์ด ์๋ค.
์๋์ฐจ๊ฐ ํฐ ์นดํ
๊ณ ๋ฆฌ์ ์๊ณ ๊ทธ ๋ฐ์ ์ ๊ธฐ์ฐจ๊ฐ ์๋ค. ํ๋ ์์์ ํตํด ์๋์ฐจ์ ๊ธฐ๋ฅ์ ์ ๊ธฐ์ฐจ๊ฐ ์ฌ์ฉํ๋๋ก ํ์.
Copy function ElectricCar (owner) {
// ์ฌ๊ธฐ์ this๋ new ElectricCar('ken'); ํ๋ฉด์ ์์ฑ๋ ๋น ๊ฐ์ฒด์ this์ด๋ค.
// Car์ ๋น๊ฐ์ฒด์ this๋ฅผ ์ค์ ํ์๋ค.
Car.call( , owner);
this.power = 0;
}
ElectricCar.prototype = Object.create(Car.prototype);
// console.log(ElectricCar.prototype.constructor) //car๊ฐ ๋์จ๋ค.
// ElectricCar.prototype = { constructor: ElectricCar };
// ElectricCar.prototype๋ก ์ ๊ทผํ ์ ์๋ ํ๋กํ ํ์
์ด๋ผ๋ ๊ฐ์ฒด๋ ํญ์ constructor๋ผ๋ ์์ฑ์
// ๊ฐ์ง๋ค. ํ์ง๋ง ์์์ ์๋ก์ด Object.create(Car.prototype);๋ฅผ ๋ง๋ค์ด์ค์
// constructor๋ฅผ ์๋๊ฒ์ด๋ค.
ElectricCar.prototype.constructor = ElectricCar;
ElectricCar.prototype.recharge = function (time) {
var that = this; // ec๋ผ๋ reference๋ฅผ ์์ฑํด์คฌ๋ค.
setTimeout(function () { // ํจ์๊ฐ ๋๋ ํ ๋น๋๊ธฐ๋ก ์คํ๋๊ธฐ ๋๋ฌธ์ this๋ฅผ ์๊ณ ๋ง๋ค.
that.power = Math.min((time / 100), 100); // ์ค์ฝํ์ฒด์ธ์ ํ๊ณ ์ฌ๋ผ๊ฐ์ that์ ์ฐพ๋๋ค.
}, time);
};
var ec = new ElectricCar('ken');
ec.soldto('aaa') // soldto๋ผ๋๊ฒ ์ค์ ๋ง๋ค์ด๋์ง ์์์ง๋ง ํ๋กํ ํ์
์ฒด์ธ์ผ๋ก ์ฌ์ฉ์ด ๊ฐ๋ฅํด์ก๋ค.
SOLID Principle(๊ฐ์ฒด์งํฅ ์ค๊ณ)
๊ฐ์ฒด์งํฅ ์ค๊ณ๋ฅผ ๋งํ๋ฉฐ, ๊ฐ์ฒด์งํฅ์ ๋ค์ฏ๊ฐ์ง ๊ธฐ๋ณธ ์์น์ ๋งํ๋ค. ์๊ฐ์ด ์ง๋๋ ์ ์ง๋ณด์์ ํ์ฅ์ด ์ฌ์ด ์์คํ
์ ๋ง๋ค๊ณ ์ ํ ๋ ์ด ์์น๋ค์ ํจ๊ป ์ ์ฉํ ์ ์๋ค.
Single responsibility principle(๋จ์ผ ์ฑ
์ ์์น)
ํ ํด๋์ค๋ ํ๋์ ์ฑ
์๋ง ๊ฐ์ ธ์ผ ํ๋ค.
Open / closed principle(๊ฐ๋ฐฉ ํ์ ์์น)
ํ์ฅ์๋ ์ด๋ ค ์์ด์ผ ํ๊ณ ๋ณ๊ฒฝ์๋ ๋ซํ ์์ด์ผํ๋ค.
Dependency inversion principle(์์กด๊ด๊ณ ์ญ์ ์์น)
ํ๋ก๊ทธ๋๋จธ๋ ์ถ์ํ์ ์์กดํด์ผ์ง, ๊ตฌ์ฒดํ์ ์์กดํ๋ฉด ์๋๋ค.???