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 JavaScript Foundations Objects Prototypes: Part 2

sunitha aravindan
PLUS
sunitha aravindan
Courses Plus Student 2,488 Points

Can i add multiple objects to another object's prototype chain?

Here is my code: var personPrototype={ name:"James", greet:function(){ name=name || "My Name"; console.log("Name is "+this.name); } } var carPrototype = { model: "generic", currentGear: 0, } function Person(name){ if(name){ this.name=name; } } Person.prototype=personPrototype; Person.prototype=carPrototype; sunu=new Person("Sunu");

In console, object sunu shows only the properties inherited from carPrototype object as below.

sunu Person {name: "Sunu", model: "generic", currentGear: 0}

How can I inherit properties from both the objects using prototype chain? Thank you!!!

2 Answers

Erik Nemesis
Erik Nemesis
13,356 Points

Yeah, Treehouse and other open classroom cannot cover the full world of Web and FrontEnd development, you will need to be curious and look into web tutorials, other projects on Github and other ebooks and keep on learning. I have five years of experience and the tools I used back then are nowhere like the tools I am using right now.

Erik Nemesis
Erik Nemesis
13,356 Points

Multiple inheritance is something forbidden in a lot of languages. The reason behind that is that if the classes inherited share a common attribute, it would be a lot confusing to know which one to take. Some languages such as C# have found workarounds to allow that anyway, but not Javascript, nor Java, PHP, Ruby...

Instead, there exists many design patterns to achieve the same result. In Java or PHP, it would be done through the use of interfaces, in Javascript however, it could be done by the use of mixins, which are "objects to be shared with other objects". Librarires such as jQuery, AngularJS or Underscore provide a simple method, extend, to do that.

So you have your Person which inherits from your Person prototype, and in that person you want to inject the prototype of Car. So using the extend method, you will do as such:

Person.prototype = $.extend(personPrototype, carPrototype, {})

Basically your carPrototype is a mixin which its properties are meant to be shared with other objects as well.

Hope that helps

sunitha aravindan
sunitha aravindan
Courses Plus Student 2,488 Points

Thanks for your response. $.extend something new I learned...and it worked for me