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

Why we use prototypes here?

I understand that we need to practice prototypes.. BUT would you do this playlist app with prototypes in real life? Seems like its just making code unreadable and complicated.. Seems like these prototype functions could be inside object...

or am I wrong?

2 Answers

Samuel Webb
Samuel Webb
25,370 Points

It would probably look more like this:

Song.prototype = {
    play: function(){
        // code
    },
    stop: function(){
        // code
    },
    toHTML: function(){
        // code
    }
}

Anyway, from what I saw the code looks pretty easy to read so it's probably fine the way it is in a small application.

Also, prototype isn't a function. The prototype of an object is basically the object that it's modeled after. So all those methods (play(), stop(), etc) that you're putting on the prototype, aren't actually on Song. They're on it's prototype object. When you call Song.play(), JS looks in the scope of Song and doesn't find it there, so it moves up to it's prototype to look for it. That's where it sees play().

Roberto Hori
Roberto Hori
5,047 Points

I think that answer is when you have an application with million of access, you need to start to think about performance, and if you create a playlist that a user click and he create a new instance, you application can be too slow. and also if you think only in client browser thats ok, but when u start to think about put this code in the server (node.js) you can have a problem.

I know this is an old comment, but how do you know how slow is slow and which method of creating a program is less slow than others?