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

Greg Schudel
Greg Schudel
4,090 Points

Why can't I call the new object instance in the person.js file?

See below commenting for what I mean

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.room = roomNumber;
}


//Teacher.prototype = Object.Create(Teacher); I know this isn't right, but it's psuedo code that I wrote quickly. why does this have to be in the teacher.js file, isn't it referenced if it was placed in either file?
GREGORY ASSASIE
GREGORY ASSASIE
3,898 Points

I hope this answers your question

The other file (Person.js) wont know about Teacher unless you explicitly expose the constructor in Teacher.js look up the module pattern

import Teacher from 'path of teacher files'
//this is done in the Person.js now you use Teacher constructor in it 
Greg Schudel
Greg Schudel
4,090 Points

The other file (Person.js) wont know about Teacher unless you explicitly expose the constructor in Teacher.js And how do I do that aside from writing the teacher.js contructor within the person.js file? I thought the javascript interpreter understands where to find any constructor in any js file (despite the fact that it is named a different js file)?

look up the module pattern what?

3 Answers

GREGORY ASSASIE
GREGORY ASSASIE
3,898 Points

If the files are linked using script tags in your html file then yes, it should find it if there is no form of encapsulation . however the order of your script tags matter. Thus teacher.js should be referenced before Person.js.

<script src='teacher.js'></script>
<script src='Person.js'></script>

SIDE NOTE The module Pattern is just a somewhat convenient way of writing JavaScript (part of es6) . modules in es6 allow you to write code separately in other files and export the code from that file.

Like I said initially . You could export Teacher.js from that file

export default Teacher ;

Then import it in Person.js

import Teacher from './teacher.js'

Now you can use Teacher constructor in Person.js

Find more About JavaScript modules Sorry about the hasty answer initially I hope this is clearer

Greg Schudel
Greg Schudel
4,090 Points

So CLEAR!! Thank you

GREGORY ASSASIE
GREGORY ASSASIE
3,898 Points

I'm glad you understood :)