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 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?