Namespacing
suggest changeECMAScript modules provide a namespace for each module: top-level declarations remain local unless they are explicitly exported.
// geometry.js
export function area(width, height) {
return width * height;
}
import * as geometry from "./geometry.js";
console.log(geometry.area(3, 4)); // 12
In a legacy classic script, an object can group related APIs and reduce the number of global names:
globalThis.myLibrary = {
area(width, height) {
return width * height;
},
};
Prefer modules for new code. A global namespace object remains useful when integrating with non-module consumers, but its name can still collide with another script and its public properties remain mutable unless protected deliberately.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents