Module Pattern Use Cases
Module Pattern Use Cases
Section titled “Module Pattern Use Cases”Introduction
Section titled “Introduction”The Module pattern is best suited for scenarios where you need encapsulation, namespace management, or controlled access to internal state.
Singleton Services
Section titled “Singleton Services”const Config = (function() { const config = { apiUrl: 'https://api.example.com', timeout: 5000, retries: 3 };
return { get(key) { return config[key]; }, set(key, value) { config[key] = value; }, getAll() { return { ...config }; } };})();Namespace Management
Section titled “Namespace Management”// Before modules — prevent global namespace pollutionconst MyApp = (function() { // All internal code is scoped here
const Utils = { formatDate(date) { /* ... */ }, debounce(fn, ms) { /* ... */ } };
const API = { async get(url) { /* ... */ }, async post(url, data) { /* ... */ } };
return { Utils, API };})();
MyApp.API.get('/users');Counter / State Management
Section titled “Counter / State Management”const Counter = (function() { let count = 0;
return { increment() { return ++count; }, decrement() { return --count; }, reset() { count = 0; }, getCount() { return count; } };})();Summary
Section titled “Summary”- Singleton services (config, logging)
- Namespace management (avoid global pollution)
- Simple state management without a framework
- Legacy codebases (pre-ES modules)