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

Matthew Day
Matthew Day
6,740 Points

Is the call method used only to access properties while prototype is primarily used to access other methods?

At around 0:30, Andrew adds Media.call(this, title, duration) to the Song constructor and then mentions that in "JavaScript, you can call any function programmatically using the call method." He continues by saying that the same parameters in Media must also be present in Song (e.g. title and duration).

At 2:20, he adds Song.prototype = Object.create(Media.prototype) to set up the prototype chain and explains that doing so "copies the references to the Media's prototype properties and methods to the song's prototype."

With this in mind, I have a few questions:

  1. Is Media.call(this, title, duration) dependent on having the prototype chain in place? In other words, if I only wanted to access the parameters in Media, can I do this without defining a prototype chain?

  2. Although Andrew states that Song.prototype = Object.create(Media.prototype) copies both "properties and methods", it seems likes its main purpose is to access methods and not properties. Is this assumption correct?

1 Answer

Steven Parker
Steven Parker
216,108 Points
  1. The .call method works on any function. You don't need to have established any prototypes.
  2. Copying methods may be the main purpose in that particular example, but it should not be assumed the main purpose for copying prototypes in general.