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

Why not use prototype for Song.isPlaying?

I understand that this.title, this.artist, and this.duration need to stay in the constructor function as they are arguments when we create a new instance of song. However I have a question about this.isPlaying:

Since using a prototype helps save on Ram/efficiency, why not use it on this.isPlaying to Song.prototype.isPlaying rather than keeping it in the constructor function to run each time a new instance of song is made?

function Song(title, artist, duration) {
  this.title = title;
  this.artist = artist;
  this.duration = duration;
//  this.isPlaying = false; //make a prototype?
}

Song.prototype.isPlaying = false; // <--

3 Answers

Raphaël Seguin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Raphaël Seguin
Full Stack JavaScript Techdegree Graduate 29,228 Points

Hi Johnatan, I think it would be wrong because you want to know if a particular instance of Song is playing so this variable should be created for every instance. You can define methods on the prototype because the function would be the same for every Song but every song has different variables. Every song has different properties but they have methods in common. Does it make sense to you ? Cheers,

Raphaël

Hi Raphael, thanks for the answer. I'm still a bit confused because, I had wrote my code with the isPlaying as a prototype and it still works. From my understanding with MDN the new already creates the property for the Song Object every time we assign a new instance to a variable and I'm able to access it and change it locally to individual vars new Song was made under.

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new

//Console Tester
function Song(title, artist, duration) {
  this.title = title;
  this.artist = artist;
  this.duration = duration;
}
Song.prototype.isPlaying = false;

var songOne = new Song()
var songTwo = new Song()

console.log(songOne.isPlaying) //returns false
console.log(songTwo.isPlaying) //returns false

//Local Changes to songOne
songOne.isPlaying = true;

console.log(songOne.isPlaying) //returns true <--
console.log(songTwo.isPlaying) //returns false

I'm still trying to grasp prototypes but I believe we are excluding functions because we don't want the functions' large code to use up compiler time every time we create a new object so we just refer to it's name. Does using prototype for properties help with efficiency by not having to run through each of them each time we create a new object?

E.g. function Object() { this.property (x 500000000 times) = value; } -- vs -- Object.prototype.property (x 5000000 times) = value

Will var object = new Object save on having to execute the 5000000 lines of this.property if I create it through prototype? Are prototyping properties efficiently done through a prototype reference or are all prototype.property declarations also being run through the compiler? (Hope this make sense, otherwise I could seriously be confusing myself haha)

Great questions! I'm waiting for answers either! Thanks! =)

Sean Smith
Sean Smith
13,797 Points

I think we want to reserve the prototype for functions, not Boolean values. isPlaying is a property of Song

Cameron Chong
Cameron Chong
4,298 Points

Jonathan Fernandez, javascript isn't a compiled language like C#. You aren't wasting compiler time because there isn't one. The reason for putting methods on the prototype is to save memory. If you want to do some googling, think of them as variables by reference, not by value.