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 trialAmmon Rock
4,457 PointsWhy do we not need to return 'this' from the Media constructor since we are not using the 'new' keyword?
I am trying to figure out why Media.call seems to automatically return 'this'. Is that just a how the 'call' method works?
1 Answer
Cale Matteson
11,303 PointsMedia.call doesn't actually return anything. What its doing is passing into Media the title and duration attributes. and setting them. Then in the Song.prototype = Object.create(Media.prototype); Its creating a Song object, with the other common attributes from the Media object. Does that make sense? I can try to clear it up more.
Good luck! Cale
Liam Cassidy
9,739 PointsLiam Cassidy
9,739 PointsHey Cale,
Thanks for the input. Here is what I have understood from your comment:
you can use call to make sure values are passed into the relevant parent objects when constructed. For example:
The call method passes in the values from the Teacher constructor to the Person constructor so that when a new teacher object is instantiated the Person object is pseudo instantiated and it has the values passed in by the call method in the teacher constructor. This is made possible via the prototype chain.
My assumption: without the prototype chain the values would still be passed to the Person constructor (via call) but the new Teacher object would have no way of accessing them.
Let me know if I'm missing something
rakan omar
15,066 Pointsrakan omar
15,066 Pointsyour downvotes are because you used to the word "creating". Song.prototype = Object.create(Media.prototype). this line does not create an instance of song, it just runs for all instances of Song. ie, when you do create an instance (or multiple instances) of song, this line will make it so that they have the properties and methods of the Media object.