Commonjs Amd,cmd

Source: Internet
Author: User

CommonJS

CommonJs is a specification for server-side modules, and node. JS uses this specification.

According to the COMMONJS specification, a single file is a module. The load module uses the Require method, which reads a file and executes it, and finally returns the exports object inside the file.

For example://Foobar.js//Private VariablesvarTest = 123; //Public MethodsfunctionFoobar () { This. Foo =function () {         //Do someing ...    }      This. Bar =function () {         //Do someing ...    } } //The methods and variables on the exports object are publicvarFoobar =Newfoobar (); Exports.foobar=Foobar;//the Require method reads the JS file by default, so you can omit the JS suffixvarTest = require ('./boobar ')). Foobar; Test.bar (); 




The CommonJS load module is synchronous, so only the loading is complete to perform subsequent operations. Like node. JS is primarily used for server programming, the loaded module files are generally already present on the local hard disk, so it is faster to load, regardless of the way asynchronous loading, so the COMMONJS specification is more applicable. However, in the case of a browser environment, to load modules from the server, this is the asynchronous pattern that must be used. So there's the AMD CMD solution.


AMD ((asynchromous Module Definition)

AMD is the normalized output of the module defined by Requirejs in the promotion process.

AMD loads the module asynchronously. Its modules support various types of modules such as Object function constructor string json.

The applicable AMD specification applies the Define method definition module.

A dependency is introduced through an array, and the callback function passes through the parameter to the dependency

define ([' SomeModule1 ', ' someModule2 '], function (SomeModule1, someModule2) {     function foo () {///         someing         Somemodule1.test ();     }     

The AMD specification allows the output module to be compatible with the COMMONJS specification, when the Define method is as follows:

Define (function  (require, exports, module) {          var reqmodule = require ("./somemodule") );     Requmodule.test ();           function  () {         //    

CMD

CMD is the normalized output of the module definition in the SEAJS process.

The difference between CMD and AMD has the following points:

1. For dependent modules AMD is executed ahead of time, and CMD is deferred execution. However, starting from 2.0, the Requirejs is also changed to be deferred (depending on how it is written, the processing does not pass).

2.CMD is highly dependent on proximity, AMD is highly reliant on pre-built.

        function ( A, b) {     //A.test (); 

Cmd

Define (function  (Requie, exports, module) {          //    var a = require (' ./a ');     A.test ();          ...      //     if  (status) {              var b = Requie ('./b ');         B.test ();     


Although AMD also supports CMD notation, relying on the predecessor is the default module definition for official documents.

The 3.AMD API defaults to one when multiple uses, the CMD strict distinction is respected as a single responsibility. For example: AMD require is global and local. CMD does not have a global require, providing seajs.use () to enable the loading of the module system to start. Each API in CMD is simple and pure.

The main differences between SEAJS and Requirejs are explained here.

Commonjs Amd,cmd

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.