Module Pattern
Module Pattern
Section titled “Module Pattern”Introduction
Section titled “Introduction”The Module pattern encapsulates private state and exposes only a public API using closures and IIFEs.
Topics Covered
Section titled “Topics Covered”- Implementation — IIFE closures and private state
- Use Cases — When to use the Module pattern
Implementation
Section titled “Implementation”const UserModule = (function() { // Private state const users = []; let nextId = 1;
function validateUser(user) { return user.name && user.email; }
// Public API return { add(name, email) { if (!name || !email) throw new Error('Name and email required'); const user = { id: nextId++, name, email }; users.push(user); return user; },
getAll() { return [...users]; },
getById(id) { return users.find(u => u.id === id); },
remove(id) { const index = users.findIndex(u => u.id === id); if (index !== -1) return users.splice(index, 1)[0]; } };})();
UserModule.add('Alice', 'alice@email.com');UserModule.getAll(); // [{ id: 1, name: 'Alice', email: 'alice@email.com' }]Summary
Section titled “Summary”- Uses closure for private state
- Returns public API object
- Pre-ES6 module pattern (still used for simple cases)