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

eaylward
eaylward
11,141 Points

How does the renderInElement method work?

Playlist.prototype.renderInElement = function(list) { list.innerHTML = ""; for (var i = 0; i < this.songs.length; i++) { list.innerHTML += this.songs[i].toHTML(); // this is the line I don't quite understand } };

How is it that the toHTML method (defined in the song.js file) can be used here? I've watched the video a handful of times and I'm still not sure why this works.

Is "this.songs[i]" technically a Song object, and therefore the toHTML method can be used? Any further explanation would be appreciated.

1 Answer

its a prototype so .renderinElement in a new method you are making to be able to reuse in the future on other objects. Its looping over the array of songs to display the songs and only the songs you have on the list.

The

list.innerHTML += this.songs[i].toHTML()

line is getting the html from the song object in the array. The [i] is just a place holder in the loop. So if you had an array of song ["Kill your heroes", "Come with me now", "Sweet Child of mine"] the first time it loops over it grabs the info form "Kill your heroes" and so on. And yes it is getting the html from the Song Object, each song in the arrary is the name of an object its accessing.

It makes it easier to add songs in the future. I hope that helps. You'll most likely end up using prototypes in the Quiz challenge as well so you'll get more practice with them.