ποΈ
μμ λ€μ΄μ€λ ν¨μν μ½λ© Chapter 4
July 27, 2023
π μμ λ€μ΄μ€λ ν¨μν μ½λ© - μ¬νν μ½λλ‘ λ³΅μ‘ν μννΈμ¨μ΄ κΈΈλ€μ΄κΈ°
μ΄ κΈμ μμ λ€μ΄μ€λ ν¨μν μ½λ© - μ¬νν μ½λλ‘ λ³΅μ‘ν μννΈμ¨μ΄ κΈΈλ€μ΄κΈ°λ₯Ό μ½κ³ μμ±ν κΈμ λλ€. ν¨μνλ ν¨μνμ΄μ§λ§, μ΄ μ± μ ν΅ν΄ μ’λ κΉλνκ³ μ§κ΄μ μΈ μ½λλ₯Ό μμ±ν μ μμ κ²μ΄λ νμλ€μ μ견μ ν λλ‘ μ΄λ² μ€ν°λ μμ μΌλ‘ μ μ νμμ΅λλ€.
Chap 4. μ‘μ μμ κ³μ° λΉΌλ΄κΈ°
- ν¨μλ‘ μ λ³΄κ° μ΄λ»κ² λ€μ΄κ°κ³ λμ€λμ§ μ΄ν΄νκΈ°
- ν μ€νΈνκΈ° μ½κ³ μ¬μ¬μ©μ±μ΄ μ’μ μ½λλ₯Ό λ§λ€κΈ° μν ν¨μν νλ‘κ·Έλλ° μ΄ν΄νκΈ°
- μ‘μ μμ κ³μ°μ λΆλ¦¬νλ λ°©λ² μκΈ°
1) λ³κ²½ μ μ½λ
let shoppingCart = []; // μ‘μ
(A): μ μλ³μλ λ³κ²½ κ°λ₯νκΈ° λλ¬Έ
let shoppingCartTotal = 0; // μ‘μ
(A): μ μλ³μλ λ³κ²½ κ°λ₯νκΈ° λλ¬Έ
const addItemToCart = (name, price) => {
shoppingCart.push({ name, price }); // μ‘μ
(A): μ μλ³μλ₯Ό λ³κ²½νκ³ μμ
calcCartTotal();
};
const updateShippingIcons = () => {
let buyButtons = getBuyButtonsDom(); // μ‘μ
(A): DOMμ μ½μ΄μ€κ³ μμ
for (let i = 0; i < buyButtons.length; i++) {
let button = buyButtons[i];
let item = button.item;
if (item.price + shoppingCartTotal >= 20) {
button.showFreeShippingIcon(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
} else {
button.hideFreeShippingIcon(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
}
}
};
const updateTaxDom = () => {
setTaxDom(shoppingCartTotal * 0.1); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
};
const calcCartTotal = () => {
shoppingCartTotal = 0; // μ‘μ
(A): μ μλ³μλ₯Ό λ³κ²½νκ³ μμ
for (let i = 0; i < shoppingCart.length; i++) {
shoppingCartTotal += shoppingCart[i].price;
}
updateTotalDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateShippingIcons(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateTaxDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
};
2) λͺ μμ μ λ ₯/μΆλ ₯, μ묡μ μ λ ₯/μΆλ ₯
- λͺ μμ μ λ ₯: ν¨μμ μΈμλ‘ μ λ¬λλ κ°
- λͺ μμ μΆλ ₯: ν¨μμ λ°νκ°
- μ묡μ μ λ ₯: μ μλ³μλ₯Ό μ½λ λ± μΈμ μΈμ λ€λ₯Έ λͺ¨λ μ λ ₯κ°
- μ묡μ μΆλ ₯: console.log(), DOM λ³κ²½, μ μλ³μ λ³κ²½ λ± λ°νκ° μΈμ λ€λ₯Έ λͺ¨λ μΆλ ₯κ°
μ묡μ μ λ ₯κ³Ό μΆλ ₯:
λΆμν¨κ³Ό
3) λ³κ²½ μ μ°¨
- κ³μ°μ ν΄λΉνλ μ½λλ₯Ό λΆλ¦¬νλ€.
- κ³μ°μ ν΄λΉνλ μ½λλ₯Ό ν¨μλ‘ μΆμΆνλ€.
- (μ²μλΆν° μ΄κ²μ κ² λ³κ²½νλ €νμ§ λ§κ³ κ·Έλ₯ κ·Έλλ‘ ν¨μλ₯Ό λΆλ¦¬νκΈ°λ§ νλ€.)
- ν΄λΉ ν¨μλ₯Ό κ·Έλλ‘ κΈ°μ‘΄ μ½λμμ νΈμΆνλ€.
- μΆμΆν ν¨μμμ μ λ ₯κ°μ μΈμλ‘, μΆλ ₯κ°μ 리ν΄κ°μΌλ‘ λ³κ²½νλ€.
3-1) λ³κ²½ μ calcCartTotal() ν¨μ
const calcCartTotal = () => {
let shoppingCartTotal = 0; // μ‘μ
(A): μ μλ³μλ₯Ό λ³κ²½νκ³ μμ
for (let i = 0; i < shoppingCart.length; i++) {
shoppingCartTotal += shoppingCart[i].price;
}
updateTotalDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateShippingIcons(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateTaxDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
};
3-2) λ³κ²½ ν calcCartTotal() ν¨μ
const calcTotal = (cart) => {
let total = 0;
for (let i = 0; i < cart.length; i++) {
total += cart[i].price;
}
return total;
};
const calcCartTotal = () => {
let total = calcTotal(shoppingCart);
updateTotalDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateShippingIcons(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
updateTaxDom(); // μ‘μ
(A): DOMμ λ³κ²½νκ³ μμ
};
Tip) μΈμλ‘ λ°λ κ°μ²΄λ₯Ό λΆλ³μ±μ μ μ§ν΄μΌνλ€. κ·Έλ μ§ μμΌλ©΄ μΈμλΌ ν΄λ λ°κΉ₯μ μ‘΄μ¬νλ κ°μ λ³κ²½ν μ μκΈ° λλ¬Έμ΄λ€.
μ£Όλ‘μΉ΄νΌ-μ¨-λΌμ΄νΈ(copy-on-write)
λ°©λ²μΌλ‘ κ°μ²΄λ₯Ό 볡μ¬ν΄μ μ¬μ©νλ€.
4) λ³κ²½ ν μ½λ
let shoppingCart = [];
let shoppingCart = 0;
const addItemToCart = ({ cart, name, price }) => {
addItem({ cart, name, price });
calcCartTotal();
};
const addItem = ({ cart, name, price }) => {
cart.push({ name, price });
};
const updateShippingIcons = () => {
let buyButtons = getBuyButtonsDom();
for (let i = 0; i < buyButtons.length; i++) {
let button = buyButtons[i];
let item = button.item;
if (isOverTwentyDollars({ item, shoppingCartTotal })) {
button.showFreeShippingIcon();
} else {
button.hideFreeShippingIcon();
}
}
};
const isOverTwentyDollars = ({ item, shoppingCartTotal }) => {
const newItem = { ...item };
return newItem.price + shoppingCartTotal >= 20;
};
const updateTaxDom = () => {
setTaxDom(calcTax(shoppingCartTotal));
};
const calcTax = (cartTotal) => {
return cartTotal * 0.1;
};
const calcCartTotal = () => {
const shoppingCartTotal = calcTotal(shoppingCart);
updateTotalDom();
updateShippingIcons();
updateTaxDom();
};
const calcTotal = ({ cart }) => {
let total = 0;
for (let i = 0; i < cart.length; i++) {
total += cart[i].price;
}
return total;
};