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 Setting Up the Prototype Chain

You're going to modify the Teacher code to inherit from the Person. First, in the Teacher constructor function, call the

please please help I have been try a lot but I cant,t pass function person(firstNam,lastnam){ this.firstName=firstName; this.lastName=lastName;

person.prototype.fullname=function(){ return this.firstName+" "+ this.lastname;

person.js
function Person(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

Person.prototype.fullName = function() {
 return this.firstName + " " + this.lastName; 
};
teacher.js
function Teacher(firstName, lastName, roomNumber) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.room = roomNumber;
}
Steven Parker
Steven Parker
231,275 Points

It doesn't look like you've written any code yet.

At least give it a good-faith attempt. If you have no idea where to start, re-watch the video on inheritance.

2 Answers

Erik Nuber
Erik Nuber
20,629 Points

The first part asks you to call the Person function and pass in the common attributes.

We no longer will need this.firstName or this.lastName as the Person function will take care of that for you.

So we call as below.

function Teacher(firstName, lastName, roomNumber) {
  this.room = roomNumber;
  Person.call(this, firstName, lastName);
}

Then you are asked to set up a prototype chain for the Teacher prototype to inherit the Person prototype

Teacher.prototype = Object.create(Person.prototype);

If you want a class to inherit the methods from another class, you need to use Object.create(), in this case, this way.

Steven Parker
Steven Parker
231,275 Points

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. :fishing_pole_and_fish:
    ― Maimonides

I HATE JavaScript. None of this makes any sense to me.

Erik Nuber
Erik Nuber
20,629 Points

Constructor functions are using prototypes can be confusing. It just takes some practice. I would recommend Javascript and Jquery by Jon Duckett. A very easy to understand book. It just takes patience and persistence.

I can explain my answer better if you would like.