0

I want to add new object of class newExpense.
expense1 is working and i can look on this in console but expense2 is not defined even when i click on button.
I need somebody to explain me how it's works and how to define expanse2 by click on button.

const addButton = document.querySelector('.add_btn');

class newExpense {
    constructor(name, date, amount) {
        this.name = name;
        this.date = date;
        this.amount = amount;
    }
}

const expense1 = new newExpense('Name', 'Some date', 'Amount');

addButton.addEventListener('click', () => {
    const expense2 = new newExpense('Name2', 'Some date2', 'Amount2');
})
Pabblo96
  • 49
  • 5
  • Your code creates a new object and assigns it to `expense2`, so it should work. How are you _accessing_ `expense2` tho? Where are you referencing/using it? – Terry Feb 17 '22 at 14:19
  • For example by `console.log(expense2)` and i see it's added in console but when i want to call this again in console writing just by `expanse2` i have a callback error `Uncaught ReferenceError: expanse2 is not defined` – Pabblo96 Feb 17 '22 at 14:27
  • Because `expense2` is oney accessible in the anonymous function and not outside of it. You need to define it outside (i.e. same level as `expense1`) – Terry Feb 17 '22 at 14:39

0 Answers0