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

can any one explain how .play() or .stop() are access here ?

 //playlist.js
function Playlist() {
   var songs = [];
   var currentIndex = 0;
}

Playlist.prototype.add = function(song) {
      this.songs.push(song);
 };

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

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

Playlist.prototype.next = function() {
   this.stop();
   this.currentIndex++;
   if(this.currentIndex===this.songs.length){
     this.currentIndex=0;
   }
   this.play();
};

Playlist.prototype.renderIn = function() {

};
  //song.js
function Song(title,artist,duration) {
  this.title = title;
  this.artist = artist;
  this.duration = duration;
  this.isplaying = false;
}

Song.prototype.play = function() {
    this.isplaying = true;
};

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

Song.prototype.toHTML = function() {

};

1 Answer

these are methods and methods are functions tied to an object.

if you type Song.play(); itll start that specific function bound to the song object.

just like a button (method) on a coffee machine (object) that starts making the coffee.

is the "var songs" is array of objects

currently its just an array [] but as soon as you see something like this

[
     {
      key: "value"
     },
     {
      key: "value"
     }
]

its an array of objects.