Mastering SeaJS: A Quick Guide to CMD‑Based JavaScript Module Loading
SeaJS is a CMD‑compliant JavaScript module loader that enables modular development across all major browsers, and this guide walks you through initializing modules, defining them with the `define` function, understanding factory parameters, and using absolute, relative, and base‑path addressing for seamless module management.
SeaJS is a JavaScript module loader that follows the CMD specification, allowing modular development and loading in all mainstream browsers.
Getting Started Example
In an HTML page, load the initial module init.js. The init module calls module1, which in turn loads module2.
Module Definition
Modules are defined with the define function: define(id?, deps?, factory) Parameters:
id (optional): the module identifier; if omitted, the file’s absolute path is used.
deps (optional): an array of module dependencies.
factory (required): the factory function that creates the module.
Example:
define('hello', ['jquery'], function(require, exports, module) {
// module code
});Factory Function
The factory function receives three arguments: require: loads dependent modules. exports: an object on which the module’s public API is attached. module: metadata about the module, including module.id, module.dependencies, and module.exports (which is the same object as exports).
Module Addressing
SeaJS supports three addressing styles:
Absolute address : a full URL, e.g., require("http://example/js/a").
Relative address : a path relative to the file containing the require call, e.g., require("../a") to load a.js from a sibling directory.
Base address : when the path is neither absolute nor starts with “./”, SeaJS resolves it against the global base configuration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
