JavaScript Modular Programming Series III: CommonJS & AMD Modular Specification Description

Source: Internet
Author: User

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
    1. Exports.add = function () {
    2. var sum = 0, i = 0, args = arguments, L = args.length;
    3. While (I < L) {
    4. Sum + = args[i++];
    5. }
    6. return sum;
    7. };

Increment.js

[JavaScript]View Plaincopy
    1. var add = require (' math '). Add;
    2. Exports.increment = function (val) {
    3. return Add (Val, 1);
    4. };

Program.js

[JavaScript]View Plaincopy
    1. var inc = require (' increment '). Increment;
    2. var a = 1;
    3. Inc (a); //2
    4. 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
      1. define ( "Alpha",  [ " Require ",  " exports ",  " beta "],  function  (Require, exports, beta)  {  
      2.      exports.verb = function ()  {  
      3.          return  beta.verb ();   
      4.          //or:  
      5.           return require ( "beta"). verb ();   
      6.      }  
      7. &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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.