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) Practice Project User Interface Code

Alex Flores
Alex Flores
7,864 Points

Object oriented vs modular programming

Hello all,

I'm just trying to conceptual grasp some JavaScript programming lingo that I was hoping someone could explain to me like I'm 5. Basically, I'm trying to understand the difference between module patterns and object oriented JS programming. I get the basics, but on a lot of websites that try to explain it, they don't explain some of the basic language. The passage below:

"The module pattern is a popular design that pattern that encapsulates 'privacy', state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private.

This provides a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface you wish other parts of your application to use. The pattern is quite similar to an immediately-invoked functional expression (IIFE) except that an object is returned rather than a function."

If you know the answers, can you please try to explain the following:

  1. "Only a public API is returned" - what does this mean exactly?
  2. "exposing an interface " - what? I've seen many websites speak of module interfaces. I don't know what an interface means when it's used in this context.

This is the websites (Module section) https://addyosmani.com/largescalejavascript/

Thanks!

1 Answer

Steven Parker
Steven Parker
229,771 Points

:point_right: The concepts of "object oriented" and "modular pattern" are compatible, not exclusive ("vs").

"Only a public API is returned" - what does this mean exactly?

This means that only the methods that are intended to be accessed by the public are available using the object that is returned. Any internal methods or variables cannot be accessed using the object.

"exposing an interface " - what? I've seen many websites speak of module interfaces. I don't know what an interface means when it's used in this context.

The "interface" (same thing as the "API") is the set of methods that are available through the returned object. So, since the object is composed of a set of accessible methods, returning the object is the same as "exposing the interface". Once you have the object, you can call the available methods as you like.

Hope that helps. :sparkles:

FYI: You might enjoy the Treehouse workshop The Module Pattern in JavaScript.

Alex Flores
Alex Flores
7,864 Points

Thanks again, Steven. This helped immensely. It feels like there are so many interchangeable words that are used among programming writers.