In Sea.js, all JavaScript modules follow the CMD (Common module definition) module definition specification. This specification clarifies the basic writing format and basic interaction rules of the module.
In the CMD specification, a module is a file. The code is written in the following format:
Define (factory);
Define
Function
define
is a global function used to define the module.
Define
define(factory)
define
Accepts a factory
parameter, either factory
as a function or as an object or as a string.
factory
As an object, a string, the interface that represents the module is the object, the string. For example, you can define a JSON data module as follows:
"Foo""Bar"});
Template modules can also be defined by a string:
Define (' I am a template. My name is {{name}}}. ');
factory
As a function, the representation is the construction method of the module. By executing this construction method, you can get the interface that the module provides to the outside. factory
when the method executes, the default is to pass in three parameters: require
,, exports
and module
:
Define (function (module) { //Module Code});
Define
define(id?, deps?, factory)
define
You can also accept more than two parameters. id
the string represents the module identity, and the array deps
is module dependent. Like what:
Define (' Hello', [' jqueryfunction (//Module Code});
id
and deps
parameters can be omitted. When omitted, it can be generated automatically through the build tool.
Note : id
deps
The use of bands and parameters define
is not part of the CMD specification, but belongs to the Modules/transport specification.
Define.cmd
Object
An empty object that can be used to determine if the current page has a CMD module loader:
if ("functiondefine. ") There are sea.js and other CMD module loaders present}
Require
Function
require
Is factory
the first parameter of a function.
Require
require(id)
require
is a method that accepts a module identity as a unique parameter that is used to obtain the interfaces provided by other modules.
Define (function (exports) { //Gets the interface of module a require ('./A.dosomething ();});
Note : At development time, require
the writing needs to follow some simple conventions.
Require.async
require.async(id, callback?)
require.async
method is used to load the module asynchronously inside the module and execute the specified callback after the load completes. callback
parameters are optional.
DefinefunctionRequireexports, module) {//asynchronously loads a module, executes callbacks when loading is complete, require. async ( "/b" function (b) {b.doSomething ();}); //asynchronously loads multiple modules, executes callbacks require. "/c" function (c, d) {c.dosomething (); d.dosomething ();});});
Note : The require
require.async
asynchronous callback executes when the synchronization is performed down. require.async
typically used to load a module that can delay asynchronous loading.
Require.resolve
require.resolve(id)
Use the path resolution mechanism inside the module system to parse and return the module path. The function does not load the module and returns only the resolved absolute path.
Define (function (exports) { console. Log (require. Resolve ('./b//==> http://example.com/path/to/b.js});
This can be used to obtain the module path, generally used in the plug-in environment or need to dynamically splice the module path of the scene.
Exports
Object
exports
is an object that is used to provide a module interface to the outside.
Define (function (exports) { //Externally provides Foo attribute exports.' Barexports. function () {};});
In addition to exports
adding members to an object, you can also use return
the direct outward interface.
Define (function (require) { ///via return directly provides interface return { foo' Bardosomething function () {}};});
If the return
statement is a unique code in a module, it can also be simplified to:
Define ({ foo' Bar', dosomethingfunction () {}});
The above format is particularly suitable for defining JSONP modules.
Special Note : The following wording is wrong!
Define (function (exports) { //ERROR usage!!! = {foo' bardosomethingfunction () {}};});
The correct notation is return
to use or assign module.exports
values:
Define (functionmodule) { module. = {Foo' bardosomethingfunction () {}};});
hint : exports
just module.exports
a reference. The factory
value is exports
not changed when it is internally re module.exports
-assigned. The exports
assignment is therefore invalid and cannot be used to change the module interface.
Module
Object
module
is an object that stores some properties and methods associated with the current module.
Module.id
String
Unique identification of the module.
Define (' idfunction (//Module Code});
define
the first parameter in the above code is the module ID.
Module.uri
String
The absolute path of the module is obtained according to the path parsing rules of the module system.
Define (functionmodule) { console. Log (module. ==> http://example.com/path/to/this/file.js});
In general (when there is no define
handwriting id
parameter in), module.id
the value is module.uri
exactly the same.
Module.dependencies
Array
dependencies
is an array that represents the dependencies of the current module.
Module.exports
Object
Interface provided externally by the current module.
factory
the argument passed to the constructor method exports
is module.exports
a reference to the object. exports
interfaces are provided only through parameters, and sometimes not all of the developer's needs are met. For example, when the interface of a module is an instance of a class, it needs module.exports
to be implemented by:
define (function (require, Exports, module) {//exports is a reference to Module.exports log (module. Exports === exports); //true //re-assigns module.exports = new someclass (); //exports no longer equals module.exports console. Log (module. Exports === exports); //false})
Note : The assignment of the pair needs to be module.exports
executed synchronously and cannot be placed in the callback function. The following is not possible:
X.jsDefine (function (setTimeout (module. = {a"Hello0);});
In Y.js there is a call to the above x.js:
Y.jsDefine (function (require ('. xConsole. Log (x.//undefined});
Summary
This is all the content of the CMD module definition specification. Frequently used APIs only define
,, require
, require.async
exports
, module.exports
these five. Other APIs have a good impression, and when needed, check the documentation without having to remember it.
The CMD specification is kept as simple as possible compared to the Requirejs AMD specification, and is very compatible with the CommonJS and node. JS Modules specifications. Modules written with the CMD specification can be easily run in node. JS and are described later.
CMD Module Definition Specification