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

Kathleen Stearns
Kathleen Stearns
1,318 Points

What am I missing?

I've entered in what I thought was the correct code, and got a syntax error that after (this) the firstName, lastName wasn't needed. When I took it out it said that it was needed. Clearly I'm missing something else, what is it?

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) {
  Person.call(this, firstName, lastName);
  this.firstName = firstName;
  this.lastName = lastName;
  this.room = roomNumber;
}

When you do

Person.call(this, firstName, lastName)

it will automatically setup

this.firstName = firstName and this.lastName = lastName

2 Answers

Maja B.
Maja B.
12,984 Points

I think you should take

this.firstName = firstName;
this.lastName = lastName;

out of Teacher constructor.

But inside of a Person constructor they should stay.

Your code is technically correct but try to think of it in terms of DRY coding. You're restating the function in there. When you call

Person.call(this, firstName, lastName);

You're running your code through the Person.js which already contains

this.firstName = firstName;
 this.lastName = lastName;

so the editor is seeing that you've both of them and is getting a little peeved, I think. I'm not sure where your error was exactly. But based on your current code, if you were to rewrite your code to demonstrate that you understand the call function as below, it should work just fine.

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