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 (2015) Constructor Functions and Prototypes Making the UI Work

matt mccherry
matt mccherry
4,493 Points

Why doesn't a setting a var to renderInElements on the buttons work?

var playlist = new Playlist()

var takeBackThisCity = new Song('Take Back This City', 'Snow Patrol', '4:23');
var intro = new Song ('Intro', 'The XX', '6:54');

playlist.add(takeBackThisCity);
playlist.add(intro);

var playlistElement = document.getElementById('playlist');
var refreshElements = playlist.renderInElement(playlistElement);


refreshElements;

var playButton = document.getElementById('play');
playButton.onclick = function() {
  playlist.play();
  refreshElements;
}
var nextButton = document.getElementById('next');
nextButton.onclick = function() {
  playlist.next();
  refreshElements;
}
var stopButton = document.getElementById('stop');
stopButton.onclick = function() {
  playlist.stop();
  refreshElements;
}

Why does this not work but having it as show below works?

//...

playlist.renderInElement(playlistElement);

var playButton = document.getElementById('play');
playButton.onclick = function() {
  playlist.play();
  playlist.renderInElement(playlistElement);
}
var nextButton = document.getElementById('next');
nextButton.onclick = function() {
  playlist.next();
  playlist.renderInElement(playlistElement);
}
var stopButton = document.getElementById('stop');
stopButton.onclick = function() {
  playlist.stop();
  playlist.renderInElement(playlistElement);
}

1 Answer

Daan Schouten
Daan Schouten
14,454 Points

Because when assigning the function to the variable, that variable will hold the function, but not run it. Essentially, you have just named the function. To execute the function, you'll have to use brackets, i.e.: refreshElements(). This should work, although I can't test from just the code you provided.

Another way to confirm that this was the issue is to run console.log(refreshElements). You'll see that it returns the function, but does not run it.

Cheers, Daan

matt mccherry
matt mccherry
4,493 Points

https://w.trhou.se/t1059aq3oq

still doesn't seem to work, console.log(refreshElements); is undefined which is super weird. Maybe you can see something im missing in the snapshot?