Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Cart() {
  2.     let items = [];
  3.  
  4.     function addItem(e) {
  5.         // @e объект события клика на кнопку
  6.         const item = {
  7.             name: e.target.parentNode.children[0].innerText,
  8.             price: e.target.parentNode.querySelector('.itemPrice').innerText
  9.         }
  10.         this.items.push(item);
  11.  
  12.         console.log('Item added:');
  13.         console.log(item);
  14.     }
  15.  
  16.     function clear() {
  17.         this.items = [];
  18.         console.log('Cart cleared');
  19.     }
  20.  
  21.     return {
  22.         addItem: addItem,
  23.         clear: clear,
  24.         items: items
  25.     }
  26. }
  27.  
  28. // создаем новую сущность для корзины
  29.  
  30. const myCart = new Cart();
  31.  
  32. // вешаем event listener'ы
  33.  
  34. [...document.getElementsByClassName('addItem')].forEach(el => {
  35.     el.addEventListener('click', (e) => {
  36.         console.log('Current cart state:')
  37.         console.log(myCart);
  38.         myCart.addItem(e)
  39.     })
  40. }
  41. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement