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

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

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

Andrew says, "This will copy the references to the media’s prototype properties and methods to the song’s prototype. This is to create the relationship between the two things right now."

Why do you need to copy these references from Media into Song? Based on the inheritance, can't Song just directly access the Media prototype methods?**

3 Answers

Seth Kroger
Seth Kroger
56,407 Points

The relevant rhetorical question here is, "How do you make prototype-based inheritance happen in JavaScript?" Any object created by the Song constructor will have Song as it's prototype, but then how do you express that Song has Media as it's prototype? One of the standard ways is through Object.create()

I like to think that ineritance will bring every element from the fathers constructor to the childs constructor. In this case, it will bring the Media attributes to the Song constructor. If you have any methods inside the constructor those methods will be available to the children as well. However, when you use prototypes to define methods they are not inside the constructor`s curly brackets, and for that reason you must create the association between parent and children using a different command, whic is the Object.create().

Andre, High five. Well explained in very simple way.

Andrew Dickens
Andrew Dickens
18,352 Points

Hi Seth and Andre, thanks for you explanations, still unsure why we need Song.prototype = Object.create(Media.prototype) ; Did we not do that with call()? What do you think of this statement?

Using call() is to share the properties and methods from one object constructor to another BUT you still need to set up a prototype chain for the js interpreter to read and link the object constructors together