Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Cart() {
- let items = [];
- function addItem(e) {
- // @e объект события клика на кнопку
- const item = {
- name: e.target.parentNode.children[0].innerText,
- price: e.target.parentNode.querySelector('.itemPrice').innerText
- }
- this.items.push(item);
- console.log('Item added:');
- console.log(item);
- }
- function clear() {
- this.items = [];
- console.log('Cart cleared');
- }
- return {
- addItem: addItem,
- clear: clear,
- items: items
- }
- }
- // создаем новую сущность для корзины
- const myCart = new Cart();
- // вешаем event listener'ы
- [...document.getElementsByClassName('addItem')].forEach(el => {
- el.addEventListener('click', (e) => {
- console.log('Current cart state:')
- console.log(myCart);
- myCart.addItem(e)
- })
- }
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement