Javascript Modular programming Series 3: commonjs & amd modular Specification Description

Source: Internet
Author: User
Commonjs module specification

The modular specification of commonjs is described inModules/1.1.1Medium

Currently, the following packages are available:

Yabble, couchdb, narwhal
(0.2 ),
Teajs (formerly v8cgi ),
Pinf JS loader,
Sorrow. js

Note that requirejs is not found here because it uses amd specifications.

The specific content defined in this Specification includes:

  • Require
    Require is a function. This function receives the identifier of a module and returns the interface of the external module. If there is a circular dependency, the external module will not execute it immediately because a passing dependency is required. In this case, the object returned by "require" must contain
    At least the exports that the foreign module has prepared before the call to require that led to the current module's execution.
    If the requested module cannot be returned, require must throw an exception.
    The require function must have the "Main" attribute.
    Must have "paths" attribute.
  • Module Context
    In a module, there is a "require" free variable.
    There is a free variable for "exports.
    There must be an "module" object variable
  • Module identifiers
    The module identifier is a string phrase separated by a forward slash.
    The phrase is named after ".", ".",
    The module ID can be "relative" or "top layer.
    Top-level identifier solves the naming of conceptual modules
    The relative identifier is resolved to the relative identifier module.
  • Unspecified
    This specification leaves some key points of interoperability uncertainty:
    Does the module need to store databases, file systems, factory function modules, and interchange link libraries?
    Is the path supported by the module loader to solve the module identification problem?

Let's take a look at the examples defined and used according to this specification:
Math. js

exports.add = function() {    var sum = 0, i = 0, args = arguments, l = args.length;    while (i < l) {        sum += args[i++];    }    return sum;};

Increment. js

var add = require('math').add;exports.increment = function(val) {    return add(val, 1);};

Program. js

var inc = require('increment').increment;var a = 1;inc(a); // 2 == "program";

Note: The above example does not actually run, because we have not implemented the require function.

AMD specifications

Currently, amd specifications are implemented as follows:
Dojo (1.7), mootools (2.0), firebug (1.8), jquery (1.7), and requirejs
AMD specifications defined in:
Amd api specifications mainly include:

  • AMD
    -Reference and define the main building blocks of modular JS Code.
    Define () function definition -- Define (ID ?, Dependencies ?, Factory );
    Id indicates the ID of the module, which is an optional parameter.

    Dependencies is a string array that indicates the identifiers of all other modules that the module depends on. The module dependency must be resolved before the actual factory method is executed. The returned values after the dependent objects are loaded and executed, the parameters of the factory method can be taken in the default order. Dependencies is also an optional parameter. If you do not provide this parameter, the default value ["require", "Exports", "module"] should be provided for the amd framework.

    Factory is a method used to execute the module change. It can use the return values of other dependent modules declared in dependencies as parameters. If this method has a return value, when this module is dependent on other modules, the returned value is the output of this module.
    Define. AMD Property
    Transporting more than one module at a time
    Let's look at an example:

    define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {     exports.verb = function() {         return beta.verb();         //Or:         return require("beta").verb();     } });
  • Require
    The API of the requrie () function allows dynamic and asynchronous loading of modules and converting the module ID to the file path.
    Require (string)
    Require (array, function)
    Require. tourl (string)

  • Loader-plugins
    Other resources that allow loading of nontraditional JS dependencies.
  • Common-config
    Optional common configurations.

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: 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.