Evolution of JavaScript Modules
The introduction of ES6/ES2015 (ECMA Script) brought native module system support to JavaScript, replacing the need for libraries like requirejs that were previously used for implementing modules in applications.
Understanding Modules
Modules allow for the export and import of classes, functions, and constants between different parts of a codebase. Anything not explicitly exported remains internal to the module it belongs to.
While TypeScript had its own concept similar to modules before ES 2015, it now aligns with the ES6 standard to ensure consistency and conformity. Learn more about this transition here.
In older applications without modules, developers relied on carefully organizing "script" elements to ensure dependencies were declared and utilized in the correct order. Variables declared in earlier script files were not overwritten by new declarations.
Understanding Classes
On the other hand, classes represent an object-oriented programming paradigm where state (fields) and behavior (functions) are encapsulated together. Access modifiers like public, private, and protected regulate how these fields can be accessed within the class or its subclasses. More details can be found here.
Key Takeaway
To recap, modules facilitate importing and using classes along with their methods and properties, while functions, constants, and enums can also be imported as needed. However, they may not offer the same level of encapsulation and abstraction provided by classes.