Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Module Pattern" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

The Module Pattern is a way to emulate classes in other programming languages. You use the Module pattern to define public/private methods and to encapsulate functionality and make more portable code.

Get Unlimited Access Now

Transcript from the "Module Pattern" Lesson

>> [MUSIC]

>> Bianca Gandolfo: So, the next thing that I wanna talk about just at a really high level is the module pattern. Module pattern, there's a little more to it, but in essence the module pattern is a way that you can modularize your code, or you can do things like make sort of class like structures in Java script.

[00:00:22] It's a way to construct sort of API which is just like an interface of interacting with your data and so, for example you can have some private methods and private properties. And then, this returns an object that has some public properties, which can be accessed by the public, and there's public methods that can be run.

[00:00:47] And then there's a privileged method that could access the private method. What this does, it limits what your application can do with the data inside of here. And this is just sort of a overview. And I'll go into more detail of how we'd actually use it. So, here's an example of a car function, a constructor function.

[00:01:13] We can create different kinds of cars like this. This would be like, if you had a car game and you didn't want people to cheat and change the gasoline level, like maybe, this maybe most boring card game ever where like you keep going until you run out of gas.

[00:01:27] [LAUGH] And so, how you would, what you would do to sort of limit that cheating is that we'd have this privileged method here that's called go which calls useGas and passes the speed. And so, what that does is that you can't change the gasoline level directly on the car, and let's just run this just to kinda look at it.

[00:01:53] So, no, my timer's still going. So, we have here our cars, so we can say like var, what's a kind of car. I don't know.
>> Speaker 2: A Ferrari.
>> Bianca Gandolfo: A Ferrari. How do you spell Ferrari?
>> Speaker 2: F-E-R-R-A-R-I.
>> Bianca Gandolfo: Okay, equals car [INAUDIBLE] take anything, no. So, now we have our Ferrari and we can look at it.

[00:02:20] So, a Ferrari is an object that has a go function, and you can change the radio station and also you can just see the radio station. And so, if we wanted to say ferrari.go(2) and we can pass our speed. So, let's we're going to, everyone goes, the speed limits about two, right?

[00:02:42] No? Okay, so quiet today you guys. And so, you can keep going until you run out of gas, and it will print out of gas. And so, we see here when we use gas, we pass it's speed and it goes up here. And useGas is a private function.

[00:03:03] So, we can't change how the gas is used. And so, this would prevent someone from cheating on the car game, where they would give themselves more gas than someone else, for example. And the whole idea here is sort of abstracting away what is important and what is not dangerous for someone to use?

[00:03:25] If you think about just the car metaphor in general, when we talk about APIs we would say, so to drive a car you don't have to know how the engine works, right? And in fact, it's better that when you're driving the car that you're not touching the engine.

[00:03:44] You're just only touching what is public to you which is the steering wheel, maybe the ignition, the shifter thing. And so, our car API is abstracting away how the engines actually working, so you can drive a car and not actually understand how the engine works and it might be dangerous if you did, and you went in there and started messing around with it, and then it broke.

[00:04:09] So, that's the same idea here. So, we have some things that are private in our car, like the gasoline level and how to use the gas in general. Whereas something that's less dangerous like changing the radio station or just going. So go, would be like pressing the ignition.

[00:04:24] We have access to that. And so, we can use closures to create an interface like that.
>> Bianca Gandolfo: Does that make sense? Thumbs on that? Thumbs?
>> Speaker 2: So, is this kinda building on the closure?
>> Bianca Gandolfo: Yeah, so this is using a closure. See how we're returning this object-
>> Speaker 2: That useGas is kinda the function within the function?

>> Bianca Gandolfo: Yeah, exactly, exactly and that's just another way to think about closure. Any questions about closures? Because we're gonna keep moving.
>> Speaker 3: To make a comment on that, where before when we were naming a function it was a global scope, this one is not, it's a function scope cuz it's defined within that function?

>> Bianca Gandolfo: Mm-hm, yeah, exactly. And you can use a module pattern to organize your whole code base into different modules as well. So, you have private components of your app that you can use. So, the module pattern is used all over and this is just a simple example of how we could use the module pattern to make classes, as like a factory to make objects, mm-hm.

>> Speaker 3: Can you call a function within one of the functions that you're returning? So for example, here could you call the go function within the change station function?
>> Bianca Gandolfo: Yep, you could.
>> Speaker 3: Okay.