1

I've written the following code and it is not working as I expected... Could you please help? I was expecting that on clicking on my button (feedCat), cat.hunger would be logged... instead, "undefined" is returned, and I can't figure out why:

 const cat = {
    hunger:100,
  
    feed() {
      console.log(this.hunger);
    }
    
}

cat.feed(); // console logs "100" as expected

const feedCat = document.getElementsByTagName("button")[0];
feedCat.addEventListener('click', cat.feed); // on click, console logs "undefined". Why not 100?!

Thanks in advance for your support!

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
KaptainIglo
  • 152
  • 1
  • 10

0 Answers0