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

TzeYang Chew
TzeYang Chew
12,039 Points

renderInElement and toHTML method confuse

Hi , i have 3 set of code here from this video.

  • can i know what is renderInElement and toHTML method and why i can't find this 2 method in mdn website
    • and what is the difference between innerHTML and toHTML as i was confuse with this code list.innerHTML += this.songs[i].toHTML(); from the video. thanks.

song.js

function Song(title, artist, duration) { //when create a function there is 2 object 1 is the Song 2nd is the prototype object
this.title = title; // a
this.artist = artist; // a
this.duration = duration; // a
this.isPlaying = false; // set the boolean to false that will be a sensible default value
}

Song.prototype.play = function() { // to acess the prototype - Song.prototype
  this.isPlaying = true;
};

Song.prototype.stop = function() {
  this.isPlaying = false;
};

Song.prototype.toHTML = function() { // n
  var htmlString = '<li';
  if(this.isPlaying) {
    htmlString += ' class="current"';
  }
  htmlString += '>';
  htmlString += this.title;
  htmlString += ' - '
  htmlString += this.artist;
  htmlString += '<span class="duration">'
  htmlString += this.duration;
  htmlString += '</span></li>';
  return htmlString;
};

playlist.js

function Playlist() {
  this.songs = []; // create an empty array for the playlist
  this.nowPlayingIndex = 0;
}

Playlist.prototype.add = function(song) { // e
  this.songs.push(song); // ascess the array with this.song // and add a new song with the push method
};

Playlist.prototype.play = function() {
  var currentSong = this.songs[this.nowPlayingIndex]; // a // to acess the current song from the array we need to pass the nowPlayingIndex
  currentSong.play(); // a // so when it play on the current playlist it will play on the current song // and when we stop the current playlist it will stop at the current song
};

Playlist.prototype.stop = function(){
  var currentSong = this.songs[this.nowPlayingIndex]; // a
  currentSong.stop(); // a
};

Playlist.prototype.next = function() { // z
  this.stop(); // stop the current song first
  this.nowPlayingIndex++; // using the increment method ++ to move along to the next song
  if(this.nowPlayingIndex === this.songs.length) { // if its equal to the song of the array we know that it has gone pass the array
  this.nowPlayingIndex = 0; // so we have to set it back to 0
}
  this.play();
};

Playlist.prototype.renderInElement = function(list) { // in the renderInElement method we can pass the variable or parameter of (list) meaning the order list from appjs
  list.innerHTML = ""; // then we can enter the list by setting the innerHTML to the empty string
  for(var i = 0; i < this.songs.length; i++) { // lets cycle over each song
    list.innerHTML += this.songs[i].toHTML(); // And then we want to add the songs HTML to the inner HTML of the list,and we can do that by doing list.innerhtml.And then we can use the += operator and this will add each of the songs HTML.
  }
}; // v

app.js

var playlist = new Playlist();

var hereComesTheSun = new Song("Here comes the Sun", "The Beatles", "2.54");
var walkingOnSunshine = new Song("Walking on Sunshine", "Katrina and the Waves", "3.43");

playlist.add(hereComesTheSun);
playlist.add(walkingOnSunshine);

var playlistElement = document.getElementById("playlist"); // get the order list with getElementById method

playlist.renderInElement(playlistElement); // we can then pass this playlistElement to renderInElement method

var playButton = document.getElementById("play"); // o
playButton.onclick = function() { // p
  playlist.play(); // p
  playlist.renderInElement(playlistElement); // l // Because each one of these methods, play, stop, and next, alters the state of the objects, we need to refresh the UI.To do that we call the rendering element method on the playlist and pass the playlist element again.
}
var nextButton = document.getElementById("next"); // o
nextButton.onclick = function() { // p
  playlist.next(); // p
  playlist.renderInElement(playlistElement); // l
}
var stopButton = document.getElementById("stop"); // o
stopButton.onclick = function() { // p
  playlist.stop(); // p
  playlist.renderInElement(playlistElement); // l
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

The "renderInElement" method is a property of the "Playlist" object and "toHTML" is a method of the "Song" object, and both are unique parts of this project. Since neither is a built-in part of the language, they won't be documented on MDN. But the definition of both of them is inside this code.

And as I mentioned already, "toHTML" is a method of the "Song" object; but "innerHTML" is property found on any HTML element. Now that one is a built-in and the documentation is on MDN.