JavaScript Object-Oriented JavaScript 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, 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
7,292 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 method is only adding the two in-common properties (title and duration).

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