ES modules and week 6 review
As a program grows, putting all the code in a single file becomes unmanageable. Modules let you split it into small files, each with one responsibility, that expose only what others need and hide the rest. ES modules are the language's standard system and work both in the browser and in Node.js. With them you avoid global variables and tangled dependencies.
export and import
With export you mark what a file offers to the outside. There are named exports, which you can have several of per file, and a default export (export default), only one. With import you bring in what you need: named exports go in braces with the same name (or renamed with as) and the default one is imported with any name you like. Relative paths must start with ./ or ../ and include the .js extension.
// math.js
export const PI = 3.14159;
export function add(a, b) {
return a + b;
}
export function multiply(a, b) {
return a * b;
}
// logger.js
export default function log(message) {
console.log(`[LOG] ${message}`);
}
// main.js
import log from './logger.js';
import { add, multiply as times, PI } from './math.js';
import * as math from './math.js';
log(add(2, 3)); // [LOG] 5
log(times(4, 5)); // [LOG] 20
log(math.PI === PI); // [LOG] trueUsing modules in Node.js and in the browser
In Node.js, the easiest way is to name files with the .mjs extension or add "type": "module" to the package.json of the folder; then you run node main.js as usual. In the browser you load the main file with a script tag with type="module". Modules run in strict mode, each file has its own scope and each module is evaluated only once even if you import it from many places. For security, the browser does not allow opening modules from file://: you need to serve the files from a local server.
<!DOCTYPE html>
<html lang="en">
<body>
<h1 id="title">Loading...</h1>
<script type="module" src="./main.js"></script>
</body>
</html>Week 6 review
- Objects group named data; they are read with dot or brackets and walked with Object.entries.
- JSON is text: stringify creates it and parse reads it (with try...catch).
- Classes are templates; extends and super allow inheritance.
- this depends on how the function is called; arrows keep the outer this.
- ES modules use export and import and keep the code tidy.
Organizing a project into modules
Good organization improves any project. A common structure consists of a main file that starts the application and several modules with clear responsibilities: one for data storage, another for helper functions and another for the visual part. Each module exports only what is needed. Avoid circular dependencies, where a imports b and b imports a, because they cause errors that are hard to understand. If two modules need each other, a third module with what they share is probably missing. With this practice your week 8 final project will be clean and easy to extend.
Teste dein Wissen
Frage 1 von 7Which word creates a new instance of a class?
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen