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

Ammon Rock
Ammon Rock
4,457 Points

Why 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
Cale Matteson
11,303 Points

Media.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

Hey 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:

function Person (name, age) {
     this.name = name;
     this.age = age;
}

Person.prototype.add_year = function () {
     this.age = this.age + 1;
};

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.

function Teacher (name, age, subject) {
     Person.call(this, name, age);
     this.subject = subject;
}

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
rakan omar
15,066 Points

your 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.