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

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

What does the "this." represent in the code?

What does the "this." represent in the code? It is the current event listener or the whole object "app.js" in itself

Could it be written like:

document.querySelector("#begin-game").style.display = "none"; instead?

3 Answers

MDN article on this question

If attaching a handler function to an element using addEventListener(), the value of this> inside the handler is a reference to the element. It is the same as the value of the currentTarget property of the event argument that is passed to the handler.

So yes, that line would be equivalent. It is the element that the listener is attached to.

Brendan Moran
Brendan Moran
14,052 Points

It should be added that as long as you use a regular old school function instead of an arrow function, this is the case.

Blaize Pennington
Blaize Pennington
13,879 Points

Brendan Moran thank you so much. I was losing my mind.

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Hamzah Iqbal , I believe in the current case that "this" refers to the actual startGameButton. So if you follow the logic here, when someone clicks the start game button, the game starts by calling

game.startGame();

then the line of code you are asking about runs

 this.style.display = 'none';
        document.getElementById('play-area').style.opacity = '1';

the "this" is pointing at the button element that the callback is created on You want to hide the button once the game has started which is exactly what it does here. It hides the start button after the game begins.

Kurt Pessa
Kurt Pessa
16,763 Points

Hamzah Iqbal Yes! I'm pretty sure you could re-select the DOM element with document.querySelector("#begin-game")

I used an arrow function, which changes the scope of this, so I used event.target to get a reference to the button being pressed!

/**
 * Listens for click on `#begin-game` and calls startGame() on game object
 */
document.getElementById('begin-game').onclick = event => {
  game.startGame();

  event.target.style.display = 'none';
  document.getElementById('play-area').style.opacity = '1';
};