As your JavaScript projects grow, organizing your code becomes crucial. Modular design patterns help you structure your code into manageable, reusable pieces. Let's explore some powerful modular techniques in vanilla JavaScript.
The Module pattern is a classic approach to creating encapsulated code. It uses an Immediately Invoked Function Expression (IIFE) to create a private scope.
const Calculator = (function() { // Private variables and functions let result = 0; function add(a, b) { return a + b; } // Public API return { sum: function(a, b) { result = add(a, b); return result; }, getResult: function() { return result; } }; })(); console.log(Calculator.sum(5, 3)); // 8 console.log(Calculator.getResult()); // 8
In this example, add
and result
are private, while sum
and getResult
are public methods.
The Revealing Module pattern is a variation that defines all functionality within the private scope and then exposes only what's necessary.
const ShoppingCart = (function() { let items = []; function addItem(item) { items.push(item); } function getItemCount() { return items.length; } function displayItems() { console.log(items); } // Reveal public pointers to private functions and properties return { add: addItem, count: getItemCount, display: displayItems }; })(); ShoppingCart.add("Apple"); ShoppingCart.add("Banana"); console.log(ShoppingCart.count()); // 2 ShoppingCart.display(); // ["Apple", "Banana"]
This pattern provides better readability as the public API is clearly defined at the end.
With the introduction of ES6, JavaScript now has built-in module support. Let's rewrite our calculator example using ES6 modules:
// calculator.js let result = 0; function add(a, b) { return a + b; } export function sum(a, b) { result = add(a, b); return result; } export function getResult() { return result; } // main.js import { sum, getResult } from './calculator.js'; console.log(sum(5, 3)); // 8 console.log(getResult()); // 8
ES6 modules provide a clean, declarative way to define module dependencies and expose functionality.
Modular design patterns are essential tools in a JavaScript developer's toolkit. They help create clean, organized, and maintainable code. Whether you choose the classic Module pattern, the Revealing Module pattern, or ES6 modules, incorporating these techniques will significantly improve your JavaScript projects.
14/09/2024 | VanillaJS
15/10/2024 | VanillaJS
22/10/2024 | VanillaJS
22/10/2024 | VanillaJS
22/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS
15/10/2024 | VanillaJS