Module Design Pattern - Beau teaches JavaScript

# The Module Design Pattern in JavaScript: Keeping Code Independent and Organized

The module design pattern in JavaScript is one of the most used design patterns for keeping particular pieces of code independent from each other. Modules allow you to break up different parts of your code, making it easier to maintain and reason about. Additionally, modules can provide encapsulation, which protects properties and functions from being accessed or modified by other parts of the code.

Before JavaScript ES6, modules were typically set up using closures. This involved declaring a function, defining private variables or functions within that function, and then returning an object containing public variables or functions. The module would be immediately invoked, creating a self-contained unit of code. While this approach works, there is now a more streamlined way to create modules using ES6 syntax and classes.

## The New Module Pattern with ES6

With ES6, the module pattern can be accomplished using classes. This allows you to keep units of code cleanly separated and organized. Instead of defining modules within closures, you can define them as classes in separate files. Each file contains a single class or set of related functions, making your codebase more modular and easier to manage.

For example, consider the following module structure:

```javascript

// shopping-list.js

class ShoppingList {

constructor() {

this.shoppingList = ['coffee', 'chicken', 'pizza']; // Private array

}

getShoppingList() {

return this.shoppingList.join(', '); // Returns a string of items separated by ', '

}

addItem(item) {

this.shoppingList.push(item); // Adds an item to the shopping list

}

}

export default ShoppingList;

````

This module defines a `ShoppingList` class with private properties and public methods. The `shoppingList` is a private array that stores the items, and the `getShoppingList()` method returns a string of these items separated by commas. The `addItem()` method allows you to add new items to the shopping list. By exporting this class as the default export, it can be imported and used in other files.

In another file, such as `main.js`, you can import the module and use its functionality:

```javascript

// main.js

import ShoppingList from './lib/shopping-list';

const myList = new ShoppingList();

console.log(myList.getShoppingList()); // Outputs: "coffee, chicken, pizza"

myList.addItem('bread');

console.log(myList.getShoppingList()); // Outputs: "coffee, chicken, pizza, bread"

```

This demonstrates how modules can be used to keep code independent and organized. Each file has a single responsibility, making it easier to maintain and debug your code.

## Benefits of Using Modules

Modules provide several benefits over monolithic code:

1. **Encapsulation**: Private properties and methods are protected from being accessed or modified by other parts of the code.

2. **Reusability**: Modules can be imported and used in multiple files, making them highly reusable.

3. **Maintainability**: By breaking code into smaller, focused modules, you can easily update or modify individual components without affecting the entire application.

4. **Scalability**: As your project grows, modules make it easier to add new features and manage existing ones.

## Conclusion

The module design pattern is a powerful tool for organizing JavaScript code. By using ES6 modules and classes, you can create clean, maintainable, and reusable components that are easy to work with. Whether you're building a small project or a large-scale application, modules help you keep your code independent, organized, and efficient.

Thank you for reading! If you found this article helpful, don't forget to share it with your fellow developers. You can also check out more resources in the description below. Remember to use your code for good!

"WEBVTTKind: captionsLanguage: enthe module design pattern in JavaScript is one of the most used design patterns for keeping particular pieces of code independent from each other modules allow you to break up different parts of your code to make it easier to maintain and reason about also modules can provide encapsulation which can protect properties and functions from being accessed from other parts of the code before JavaScript es6 you would set up a module like this you would um declare this function you would have the private variables or functions up here and then you would return what you wanted your public variables or functions to be and then it would just be immediately invoked right here this is just a closure that returns an object I'm not going to go into much detail about this because there is a new way to do it with es6 the module pattern can be accomplished using classes this allows you to keep units of code cleanly separated and organized so with es6 our modules will look something like this now the whole point of modules is that you're keeping code separ seated usually in different files so I don't have different files on here but this is representing one file here and this uh this down here main.js is representing another file so let's look through this we have a class shopping data type and we have a Constructor these are going to be the private properties this. shopping list equals coffee chicken pizza so we're creating an array that that's a private array now we also have these public methods um get shopping list where it's not going to return the actual array it's actually going to return a string that join is just going to return a string with all these item with all the items in the array separated by a comma and a space so you cannot have access to the actual array and then we can also add an item this that function here it's just going to push the item onto the array so at the end of the file we have to do this line export default shopping data type so that's just going to export this class so we can use it in a different file at the beginning of the other file we're going to put import shopping data type from lib slm module that's just the directory and file name without the JS and after we import that in we can start using the file so we can do something like and this would allow you to um print the shopping list in the other file so this is just a simple example of a module but they can be pretty complex and just allow you to keep code independent from each other in different files thanks for watching my name is Bo K check the description for links some more information don't forget to subscribe and remember use your code for goodthe module design pattern in JavaScript is one of the most used design patterns for keeping particular pieces of code independent from each other modules allow you to break up different parts of your code to make it easier to maintain and reason about also modules can provide encapsulation which can protect properties and functions from being accessed from other parts of the code before JavaScript es6 you would set up a module like this you would um declare this function you would have the private variables or functions up here and then you would return what you wanted your public variables or functions to be and then it would just be immediately invoked right here this is just a closure that returns an object I'm not going to go into much detail about this because there is a new way to do it with es6 the module pattern can be accomplished using classes this allows you to keep units of code cleanly separated and organized so with es6 our modules will look something like this now the whole point of modules is that you're keeping code separ seated usually in different files so I don't have different files on here but this is representing one file here and this uh this down here main.js is representing another file so let's look through this we have a class shopping data type and we have a Constructor these are going to be the private properties this. shopping list equals coffee chicken pizza so we're creating an array that that's a private array now we also have these public methods um get shopping list where it's not going to return the actual array it's actually going to return a string that join is just going to return a string with all these item with all the items in the array separated by a comma and a space so you cannot have access to the actual array and then we can also add an item this that function here it's just going to push the item onto the array so at the end of the file we have to do this line export default shopping data type so that's just going to export this class so we can use it in a different file at the beginning of the other file we're going to put import shopping data type from lib slm module that's just the directory and file name without the JS and after we import that in we can start using the file so we can do something like and this would allow you to um print the shopping list in the other file so this is just a simple example of a module but they can be pretty complex and just allow you to keep code independent from each other in different files thanks for watching my name is Bo K check the description for links some more information don't forget to subscribe and remember use your code for good\n"