ννμμ μ΄λ¦μ λΆμ΄κ³ μΆλ€.
νμ¬ ν¨μ μμμλ§ μλ―Έκ° μλ€? : λ³μλ‘ μΆμΆ
ν¨μλ₯Ό λ²μ΄λ λμ λ¬Έλ§₯μμκΉμ§ μλ―Έκ° μλ€? : ν¨μλ‘ μΆμΆ_ μ§μν¨μλ‘μ μ¬μ©
1. μΆμΆνλ €λ ννμμ λΆμμ©μ μλμ§ νμΈνλ€.
2. λΆλ³ λ³μλ₯Ό νλ μ μΈνκ³ μ΄λ¦μ λΆμΌ ννμμ 볡μ λ³Έμ λμ νλ€.
3. μλ³Έ ννμμ μλ‘ λ§λ λ³μλ‘ κ΅μ²΄νλ€.
4. ν μ€νΈνλ€.
5. ννμμ μ¬λ¬ κ³³μμ μ¬μ©νλ€λ©΄ κ°κ°μ μλ‘ λ§λ λ³μλ‘ κ΅μ²΄νκ³ , κ·Έλλ§λ€ ν μ€νΈνλ€.
νμ¬ ν¨μ μμλ§ μλ―Έκ° μλ€ -> λ³μλ‘ μΆμΆ
// λ³κ²½μ
function price(order){
// κ°κ²©(price) = κΈ°λ³Έ κ°κ²© - μλ ν μΈ + λ°°μ‘λΉ
return order.quantity*order.itemPrice
- Math.max(0,order.quantity-500)*order.itemPrice*0.05
+ Math.min(order.quantity*order.itemPrice*0.1,100);
}
// λ³κ²½ν
function price(order){
const basePrice = order.quantity*order.itemPrice;
const quantityDiscount = Math.max(0, order.quantity-500)*order.itemPrice*0.05;
const shipping = Math.min(basePrice*0.1,100)
return basePrice - quantityDiscount + shipping;
}
price() λ©μλμ λ²μλ₯Ό λμ΄ μ£Όλ¬Έμ νννλ Order ν΄λμ€ μ 체μ μ μ©
/*
μ΄λ¦μ΄ κ°κ²©μ κ³μ°νλ price()μ λ©μλ λ²μλ₯Ό λμ΄,
μ£Όλ¬Έμ νννλ order ν΄λμ€ μ 체μ μ μ©λλ€.
*/
class Order {
constructor(aRecord){
this._data = aRecord;
}
get quantity() {return this._data.quantity;}
get itemPrice() {return this._data.itemPrice;}
get price(){
return this.quantity*this.itemPrice
- Math.max(0, this.quantity-500)*this.itemPrice*0.05
+ Math.min(this.quantity*this.itemPrice*0.1, 100);
}
}
// λ³κ²½ν ==> ν¨μλ‘ μΆμΆνκ³ μ§μν¨μλ‘μ μ¬μ©νλ€.
class Order {
constructor(aRecord){
this._data = aRecord;
}
get quantity() {return this._data.quantity;}
get itemPrice() {return this._data.itemPrice;}
get price(){
return this.basePrice - this.quantityDiscount + this.shipping ;
}
get basePrice(){return this.quantity*this.itemPrice}
get quantityDiscount(){return Math.max(0, this.quantity-500)*this.itemPrice*0.05}
get shipping(){return Math.min(this.quantity*this.itemPrice*0.1, 100)}
}
| 6.8. λ§€κ°λ³μ κ°μ²΄ λ§λ€κΈ° (Introduce Parameter Object) (0) | 2021.09.26 |
|---|---|
| 6.6. λ³μ μΊ‘μννκΈ° (Encapsulate Variable) (0) | 2021.09.26 |
| 6.1. ν¨μ μΆμΆνκΈ° (0) | 2021.09.26 |
| 9.2. νλ μ΄λ¦ λ°κΎΈκΈ° (Rename Field) (0) | 2021.09.25 |
| 6.7. λ³μ μ΄λ¦ λ°κΎΈκΈ° (Rename Variable) (0) | 2021.09.25 |