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

Erik Nuber
Erik Nuber
20,629 Points

Aide for better understanding Object.create();

Another topic that is very confusing. I spent a lot of time looking up information that just added to the confusion or explained what I was already confused about without actually telling me what the line of code was doing.

I am just sharing a link that helped explain Object.create() it in a way that really made sense to me and, thought it might be helpful to others.

http://www.bennadel.com/blog/2184-object-create-improves-constructor-based-inheritance-in-javascript---it-doesn-t-replace-it.htm

Just as a side note, the information in the link is several years old and, mentions things like Object.create() not being available in all browsers and needing to add in .init() after to get things to work properly. I didn't see this info elsewhere so may not be true anymore however, I still found the explanation to be excellent in explaining the material in a way that is much more understandable than anything else I read.

2 Answers

Assuming that you know Object.getPrototype() to find out the prototype of the object, given a song object created from Song, you have:

Object.getPrototype(song) === Song.prototype

is ture which you can test in the console.

Once Song.prototype = Object.create(Media.prototype) is issued, you have:

Object.getPrototype(Song.prototype) === Media.prototype;

is ture. But without Song.prototype = Object.create(Media.prototype), you have:

Object.getPrototype(Song.prototype) === Media.prototype;

is false and you have instead:

Object.getPrototype(Song.prototype) === Object.prototype;

is ture. Object.prototype is the eventual prototype of any objects. In fact:

Object.getPrototype(Media.prototype) === Object.prototype;

is true. Without explicitly declaring the prototype chain, the chain does not go around Media.prototype from Song.prototype before ending up to Object.prototype.

In real life, your child inherits some traits from you and your child inherits some traits from your parents through you automatically. In Javascript life, your child inherits some traits from you, but you have to declare that your child also inherits some traits from your parents even if you give birth to him.

In Javascript you can change your parents or your child's grandparents to some strangers because the inheritance does not get chained through automatically. For example:

Song.prototype = Object.create(Animal.prototype);

will allow a song object to use methods on the Animal prototype instead of the Media.prototype even if this song object has been created using Media.call(this,...). The Javascript seems to allow it.

Great resource! Thanks Anthony.