function add_item_to_cart(shopping_cart, name, price) {
shopping_cart = add_item(shopping_cart, name, price);
calc_cart_total(shopping_cart);
}
function calc_cart_total(cart) {
cart_total = calc_total(cart);
set_cart_total_dom(cart_total);
update_shipping_icons(cart);
update_tax_dom(cart_total);
}
function set_cart_total_dom(cart_total) {
...
cart_total
...
}
function update_shipping_icons(cart) {
var buy_buttons = get_buy_buttons_dom();
for (var i = 0; i < buy_buttons.length; i++){
var button = buy_buttons[i];
var item = button.item;
var new_cart = add_item(cart, item.name, item.price);
if(gets_free_shipping(new_cart)) {
button.show_free_shipping_icon();
} else {
button.hide_free_shipping_icon();
}
}
}
function update_tax_dom(cartTotal) {
set_tax_dom(calc_tax(cartTotal));
}
// before
function add_item(cart, item) {
var new_cart = cart.slice();
new_cart.push(item);
return new_cart;
}
// after
function add_element_last(array, item) {
let new_array = array.slice();
new_array.push(item);
return new_array;
}
범용성있는 변수명으로 변경하여 장바구니에 제품을 추가하는 함수
에서 배열에 새 요소를 주입하는 함수
로 변경하여 재사용성을 높였다.
// before
function update_shipping_icons(cart) {
var buy_buttons = get_buy_buttons_dom();
for (var i = 0; i < buy_buttons.length; i++){
var button = buy_buttons[i];
var item = button.item;
var new_cart = add_item(cart, item.name, item.price);
if(gets_free_shipping(new_cart)) {
button.show_free_shipping_icon();
} else {
button.hide_free_shipping_icon();
}
}
}
// after
function update_shipping_icons(cart) {
var buy_buttons = get_buy_buttons_dom();
for (var i = 0; i < buy_buttons.length; i++){
var button = buy_buttons[i];
var item = button.item;
const is_free_shipping = get_free_shipping_with_new_item(cart, item);
set_free_shipping_icon(is_free_shipping);
}
}
function get_free_shipping_with_new_item(cart, item) {
let new_cart = add_item(cart, item);
return gets_free_shipping(new_cart)
}
function set_free_shipping_icon(is_free_shipping) {
if(is_free_shipping) {
button.show_free_shipping_icon();
} else {
button.hide_free_shipping_icon();
}
}