Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Playlist Project

clemhlrdt
clemhlrdt
20,534 Points

Why do the function is called by itself here ?

In the playlist.js file, we create a constructor and define properties.

With prototype, we define Methods like this:

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

It is a bit confusing for me. I understand the role of prototype here BUT I don't understand the fact that we call the play function on the variable currentSong.

1 Answer

Hello there,

In this project, 'Song' inherits from 'Media', which has a 'play' method. In media.js, we can see that the play method sets the 'isPlaying' property to 'true'. Then, back in song.js, we can see that the toHTML function checks for isPlaying. If it's true, it gives that song the class of 'current', which styles it differently. Basically, in this case, calling the 'play' method on the song indicates that it should be styled in a way that visually separates it from the other playlist items that are not considered to be playing at that time.

Hope this helps!

clemhlrdt
clemhlrdt
20,534 Points

Thank you Katie Wood :) Love the shiba inu btw.

Haha, thank you! :)