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

I'm stuck. Please help.

Challenge: You're going to modify the Teacher code to inherit from the Person. First, in the Teacher constructor function, call the Person constructor, using the call method and pass in the common attributes.

I don't know what I'm doing.

Any suggestions?

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

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;
}

4 Answers

part 1 of 2

this is the right code for the Teacher.js file

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

however in the Person.js code should have not been touched and should look like this.

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

Person.prototype.fullName = function() {
 return this.firstName + " " + this.lastName; 
};

i would recommend that you rewatch the three videos proceding this quiz, they are not very long and it took me about 3-4 times through them to see what was actually going on.

part 2 of 2

then the next part of the quiz asks you to link the Person.js and Teacher.js. this is covered in the third video and again i would recommend watching it till it makes sense, however the code will look like this.

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

and it goes in the Teacher.js file. hope this helps and happy coding.

Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

I believe that watching them again with more atention solved this for me.

Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

I believe that watching them again with more atention solved this for me.

It does not work at all.

In the Teacher.js file the following code is wrong:

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

It should be this :

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

alexlitel
alexlitel
25,059 Points

Ja'Nel Johnson You seem to have run into an error changing the person.js file. You actually have the right code for the Teacher function, but it belongs in the teacher.js file. You need to keep the original function for Person intact because that is what Teacher—being a type of Person—inherits from. The Person.call method allows us to reuse—or inherit—the properties from a person. Nothing in this challenge requires us to change the person.js file.

You then follow this up by establishing a Teacher.prototype and setting it equal to a Object.create method that allows us to also create a new Person when we create a new Teacher.

Thanks so much! I realized what I had done wrong after I posted the question.

No wonder it didn't go through.