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

Igor Yamshchykov
Igor Yamshchykov
24,397 Points

Clarify the prototype chain

I just want to clarify if I understood it correctly. So when I'm doing

function Song(title, artist, duration) {
  Media.call(this, title, duration);
  this.artist = artist;
}

I actually don't create an instance of Media, I'm just setting the Song properties through the Media object ? Is that correct ? Is it like inheritance ?

2 Answers

Media.call(this, title, duration) calls Media with a different value for this. I'm assuming that Media is acting as a constructor, and it'll set the title and duration properties in the Song object. To create a new instance of Media you would have to use new.

Ernest Son
Ernest Son
13,532 Points

So, why isn't this line

Media.call(this, title, duration);

actually this

Media.call(this)

Isn't title and duration = this ?

Thanks

Because this refers to the current object of the Song constructor function, whereas title and duration are the properties of the Media constructor. So, when we make a call to the Media constructor, we not only pass the current object, but also the values for the title and duration properties as arguments.

What we are trying to achieve here, is to inherit certain properties from the Media constructor function, as Song (also a constructor function) doesn't have it. This helps us with our DRY principle.

John Paul Naranjo
seal-mask
.a{fill-rule:evenodd;}techdegree
John Paul Naranjo
Full Stack JavaScript Techdegree Student 2,090 Points

DRY principle ( for those of you who are new ) means Don't Repeat Yourself. It's means don't do things again and agajn, which you can do once.