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) Constructor Functions and Prototypes Creating Multiple Instances with Constructors

Constructor functions are like Classes, then?

Classes in other Object Oriented languages are like the blueprints of objects. In JavaScript, however, it seems Constructor functions are these blueprints.

When I think of designing an O-O project, I soon think of classes. How are complex projects designed in JavaScript? It looks like I don't need to create different files for different objects, what makes the project's code go in one file... I'm confused...

2 Answers

I find it funny that you are asking this question, just moments after I posted this question: https://teamtreehouse.com/forum/why-are-you-teaching-about-constructor-functions

Constructor functions are at best folly. Read up on OLOO (Objects that Link to Other Objects), the "two pillars" of JavaScript and how ECMA6 including the "Class" type is laughable.

Google all of that and if your understanding of fundamental JS is fair, you should understand why constructors aren't like classes at all, and that class inheritance is inferior to prototypal inheritance, which is why JavaScript is the most versatile programming language out there.

"which is why JavaScript is the most versatile programming language out there."... it has to do with JavaScript's elegant combination of OOP (Object-Oriented) and FP (Functional Programming)

Rachelle Wood
Rachelle Wood
15,362 Points

I admit that my Java knowledge is shakey, but isn't a JS constructor function just like a Java constructor function? I mean, they are factory functions. They create an instance of a particular object from a class that has been defined elsewhere. As to your question about why you don't need to make a new file per class like in Java, a partial answer would be that it is because JS is a scripting language. You program within a given framework. That framework is the window object. I don't see why you couldn't program something really complex with JS, but is it worth it? Here is some more about this subject: http://www.quirksmode.org/js/intro.html