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 Building the Movie Object

Erin McCulloch
Erin McCulloch
10,098 Points

Seperate .js files?

In all of the videos and quizzes for the constructor functions and prototypes section, all of the pieces of code have been very separated with different .js files for Media, Song, and Movie. Can these all be in the same .js file as long as they are in the same order as they are being called so they inherit properly? This hasn't been mention at all as to why this is being done in the videos so it's not clear if there has to be separate files for each.

2 Answers

Seth Kroger
Seth Kroger
56,414 Points

While you can put all the code in a single JavaScript file, it's best practice to put each type of object into it's own separate file. It's easier to find the code responsible for each object by just looking up one small file instead of hunting around in one large one. It makes it easier to reuse the object, since all the code for it is there but no a lot of extraneous stuff. And it helps keep the code isolated (or encapsulated), and you can wrap it inside a module pattern or ES6 modules to isolate it further and package it up for reuse.

While it may be easier to include a single script in a webpage instead of many, there are tools available that can bundle up the separate files for you, like webpack.

Pavel Raav
Pavel Raav
7,811 Points

In addition to Seth's answer, I want to point out, that the reason for this actually was mentioned a few videos ago...