Chapter 8: The Module Pattern
Encapsulation and Least Exposure (POLE)
The bundling or co-location of information (data) and behavior (functions) that together serve a common purpose.
The control of visibility of certain aspects of the encapsulated data and functionality.
The recent trend in modern front-end programming to organize applications around Component architecture pushes encapsulation even further.
What Is a Module?
A module is a collection of related data and functions (often referred to as methods in this context), characterized by a division between hidden private details and public accessible details, usually called the "public API."
Namespaces (Stateless Grouping)
If you group a set of related functions together, without data, then you don't really have the expected encapsulation a module implies.
Data Structures (Stateful Grouping)
Even if you bundle data and stateful functions together, if you're not limiting the visibility of any of it, then you're stopping short of the POLE aspect of encapsulation.
Modules (Stateful Access Control)
To embody the full spirit of the module pattern, we not only need grouping and state, but also access control through visibility (private vs. public).
Revealing Module
Only properties added to the public API object returned from the function will be exported for external public use.
Module Factory (Multiple Instances)
We call the module factory, producing an instance of the module that we label fullTime
. This module instance implies a new instance of the inner scope, and thus a new closure that getName(..)
holds over records
.
Classic Module Definition
There must be an outer scope, typically from a module factory function running at least once.
The module's inner scope must have at least one piece of hidden information that represents state for the module.
The module must return on its public API a reference to at least one function that has closure over the hidden module state (so that this state is actually preserved).
Node CommonJS Modules
CommonJS modules are file-based; one module per file.
The records
and getName
identifiers are in the top-level scope of this module, but that's not the global scope. Everything here is by default private to the module.
To expose something on the public API of a CommonJS module, you add a property to the empty object provided as module.exports
.
CommonJS modules behave as singleton instances, similar to the IIFE module definition style presented before.
Modern ES Modules (ESM)
ESM files are assumed to be strict-mode, without needing a "use strict"
pragma at the top.
The getName
identifier is function hoisted, so it's available throughout the whole scope of the module:
"namespace" import:
Last updated