CommonJS Module Specification
The modular specification of the CommonJS is described in the modules/1.1.1
The packages currently implementing this specification are:
Yabble,couchdb,narwhal (0.2), Wakanda, Teajs (formerly v8cgi), Commonscript, Pinf JS Loader, Seajs, Arangodb, sorrow.js
Note that Requirejs is not found here because it uses the AMD specification.
Specific content defined by this specification includes:
- Require
Require is a function. This function takes the identity of a module and returns the interface of the external module. If there is a cyclic dependency, the external module will not execute immediately, because a transitive dependency is required; In this case, the object returned is "require" must contain at least the exports Foreign module has prepared before, the call to require, the led to the current module ' s execution.
If the requested module cannot be returned, require needs to throw an exception.
The Require function must have a "main" attribute.
There must be "paths" attribute.
- Module Context
In a module, there is a free variable of "require".
There is a free variable of "exports".
Must have an object variable of "module"
- Module Identifiers
The module identifier is a string phrase that is separated by a forward slash.
Phrases using the Hump method, ".", "..." Named
The identity of the module can be either "relative" or "top-level".
Top-level identification solves the naming of conceptual modules
Relative identifiers are resolved relative to the identifier module.
- Unspecified
This specification leaves behind some of the key points of interoperability uncertainties:
Does the module need to store database, file system, factory function module, Interchange link library?
is the path supported by the module loader to solve the problem of module identification?
Take a look at examples that are defined and used in accordance with this specification:
Math.js
[JavaScript]View Plaincopy
- Exports.add = function () {
- var sum = 0, i = 0, args = arguments, L = args.length;
- While (I < L) {
- Sum + = args[i++];
- }
- return sum;
- };
Increment.js
[JavaScript]View Plaincopy
- var add = require (' math '). Add;
- Exports.increment = function (val) {
- return Add (Val, 1);
- };
Program.js
[JavaScript]View Plaincopy
- var inc = require (' increment '). Increment;
- var a = 1;
- Inc (a); //2
- Module.id = = "program";
Note that the above example does not actually run, because we do not implement the Require function.
AMD specifications currently implement the AMD specification are:
Dojo (1.7), MooTools (2.0), Firebug (1.8), JQuery (1.7), REQUIREJS, etc.
AMD's specifications are defined in: Https://github.com/amdjs/amdjs-api/wiki
The AMD API specification mainly includes:
- Amd
-Reference and define the main building blocks of the modular JS code.
The definition of the Define () function--Define (ID, dependencies, Factory);
The ID represents the identification of the module, which is an optional parameter.
Dependencies is a string array that represents all of the other module identifiers that the module relies on, and the module dependencies must be resolved before the concrete factory method is actually executed, and these dependent objects are loaded to perform subsequent return values, which can be used in the default order as parameters of the factory method. Dependencies is also an optional parameter, when the user does not provide this parameter, the implementation of the AMD framework should provide default values of ["require", "exports", "module"].
Factory is a method for executing a module, which can use the return value of other dependent modules declared in the previous dependencies as parameters, if the method has a return value, when the module is dependent on other modules, the return value is the output of the module.
Define.amd Property
Transporting more than one module at a time
See an example:
[JavaScript]View Plaincopy
- define ( "Alpha", [ " Require ", " exports ", " beta "], function (Require, exports, beta) {
- exports.verb = function () {
- return beta.verb ();
- //or:
- return require ( "beta"). verb ();
- }
- &NBSP;});
- Require
The Requrie () function API allows dynamic, asynchronous loading of modules and resolves module IDs to identify strings to file path conversions.
Require (String)
Require (Array, Function)
Require.tourl (String)
- Loader-plugins
Other resources that are allowed to load non-traditional JS dependencies.
- Common-config
An optional common configuration.
http://blog.csdn.net/oscar999/article/details/8907087
JavaScript Modular Programming Series III: CommonJS & AMD Modular Specification Description