Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript: Challenge Rendering the Game Connecting with the DOM Solution

Alexandra Lukinicheva
Alexandra Lukinicheva
7,901 Points

Uncaught TypeError: Cannot set property 'display' of undefined. Why does 'this' come out undefined?

I think I followed this step closely, but not sure why the console throws an error on click...

const game = new Game(); /**

  • Listens for click on #begin-game and calls startGame() on game object */ document.getElementById('begin-game').addEventListener("click", () =>{ game.startGame(); this.style.display = 'none'; document.getElementById('play-area').style.opacity = '1'; });

Why does 'this' come out undefined?

Alexandra Lukinicheva
Alexandra Lukinicheva
7,901 Points

I see that issue was that I'm using an arrow function, but I still would like to know why it doesn't work with the arrow function?

1 Answer