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 Playlist Project

Anthony c
Anthony c
20,907 Points

How does this.stop on Playlist.prototype.next know it's stopping the current song?

how does the below function know that this.stop(); is referring to the current song? Why do we need to get the currentSong for stop() and play() but not for next()?

Is it because stop() already identifies currentSong? so this.stop() in the next() method is essentially currentSong.stop?

Playlist.prototype.next = function() {
  this.stop();
  this.nowPlayingIndex++;
  if (this.nowpLayingIndex === this.songs.length) {
    this.nowPlayingIndex = 0;
  }
  this.play;
};
rydavim
rydavim
18,813 Points

I've edited your question to include code formatting. You can do this using the following markup:

Adding Code to Treehouse Posts

3 Answers

rydavim
rydavim
18,813 Points

next is calling the stop and play methods. These methods are what is selecting the song to be stopped or played.

Playlist.prototype.play = function() {
  var currentSong = this.songs[this.nowPlayingIndex]; // This is how it knows it's referring to the current song.
  currentSong.play();
};

next is instead manipulating the nowPlayingIndex, which is how we know what song we're on. So it doesn't need to know what the current song is, only what the index should be.

Hopefully that helps, but let me know if something still isn't clear. Happy coding! :)

Thanks. Went through countless asnwers on the forum to finally get one that made sense and crystallized things for me. :)

You are confused in interpreting .stop() method for Song object and for Playlist object. They work differently or better to say they are completely different. Their methods just have the same name. Let song = new Song() and playlist = new Playlist().

song.stop() makes sense to you and you are saying playlist.stop() doesn't make sense. But playlist.stop() does not work the same way as song.stop().

playlist.stop() is defined in playlist.js as:

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

When playlist.stop is called, .stop() gets a list of songs and the index of currently-playing song through "this".

I think the confusion will disappear if you name it differently like:
Playlist.prototype.stopCurrentSong = function(){
var currentSong = this.songs[this.nowPlayingIndex];
currentSong.stop();
};

Jonah Shi
Jonah Shi
10,140 Points

I have the same question, don't understand this.stop() is referring to the the Index? does "this" not refer to the playlist object? If so, how can we stop the whole list?