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
brandonlind2
7,823 PointsWhy does media.call() need more arguments then 'this'?
I'm a bit confused as to way media.call, needs the arguments of the inherited constructor function, wouldn't the arguments of the constructor that is inheriting be used? I understand way the 'this' argument is needed in media.call but I don't understand why title and duration is needed.
I'm trying to understand how the code works, I'm having trouble seeing the value of he arguments of the inherited constructor function other the 'this', why wouldnt this this work?
function song (title, artist, duration){ media.call(this); this.artist= artist; }
1 Answer
Balázs Buri
8,799 Pointsin your code:
function Song(title, artist, duration) { //title and duration parameters unused inside this function
Media.call(this);
this.artist = artist;
}
you create the class Song (you should capitalize class names) which will extend the Media class. You don't use the title and duration parameters anywhere inside your function however so they are lost forever. Those parameters aren't passed automatically to the parent Media. In the correct code:
function Song(title, artist, duration) {
Media.call(this, title, duration);
this.artist = artist;
}
the parameters are passed to Media. But because this is now the song, inside Media:
this.title = title; will mean song.title = title
also it's the only place where the parameter is finally stored.
hope this helps