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) Prototypal Inheritance Updating the Song Object

Roudy Antenor
Roudy Antenor
4,124 Points

Why not just add the play() and stop() methods within the media prototype build? Why make them prototypes of media??

Look to time 2:06 and you will see that instead of adding the play() and stop() methods to the media constructor prototype they were instead left out of the build, and made to be prototypes of the Media prototype ?? any reason why this was done??

when this was done at time 4:07:

Song.prototype = Object.create(Media.prototype);

was it done to add the play() and stop() methods to Song as prototypes of the Media constructor? Wasn't this done when we placed Media.call(this, title, duration) in our Song constructor? or was the title and duration properties the only thing added when we did that?

1 Answer

Steve Gallant
Steve Gallant
14,943 Points

I will attempt to answer, though I am still working to grasp all this myself.

At time 2:06, he is working with the Media constructor function, not a prototype. If he adds the play() and stop() methods into the function, more memory will be consumed by the page for each instance of Media objects that are created. Instead, he leaves those methods in the Media.prototype so that they are globally available (through inheritance) to all instances of Media objects without using more RAM.

At 4:07, yes I believe you are correct - it is to inherit the play() and stop() methods on the Song.prototype from the Media.prototype. The Media.call method is only adding the two in-common properties (title and duration).

Hope this helps (and that I didn't get anything incorrect)! Steve