Module specification in JS (commonjs,amd,cmd)

Source: Internet
Author: User

AMD cmd differences

Concept:

Yuber's explanation for the difference between AMD and CMD: AMD is a normalized output of the module defined by REQUIREJS during the promotion process. CMD is the normalized output of the module defined by SEAJS in the process of generalization. AMD It is a modular development in the browser-side specification asynchronous module definition, in the vernacular is the asynchronous module definition, for Jser, async is no longer familiar with the word, all modules will be asynchronously loaded, module loading does not affect the subsequent statement run.  All statements that depend on certain modules are placed in the callback function. Difference

1. For dependent modules, AMD is executed in advance and CMD is deferred . However, starting from 2.0, Requirejs is also changed to be deferred (depending on the way it is written and handled differently). CMD respected as lazy as possible.

2. CMD is highly dependent on the nearest , AMD is highly dependent on the predecessor . Look at the code:

Cmd
Define (function (Require, exports, module) {
var a = require ('./a ')
A.dosomething ()
100 lines are omitted here
var B = require ('./b ')//dependency can be written near
B.dosomething ()
// ...
})

The default recommended by AMD is
define (['./a ', './b '), function (A, B) {//dependencies must be written at the beginning.
A.dosomething ()
100 lines are omitted here
B.dosomething ()
...
})

Requirejs and seajs The mechanism of executing the module is very different.

Seajs's attitude towards the module is lazy execution, while Requirejs's attitude to the module is pre-executed

Requirejs will execute the (dependent) module as soon as possible, as if all the Require are advanced, (Requirejs is to load all dependent modules in parallel and parse before starting to execute other code) and the order of the modules is not necessarily 100% is to Mod1 and Mod2 first.

So you see that the order of execution is completely different from what you expected! Shake It ~ requirejs!

Seajs, in parallel, loads all dependent modules, but does not immediately execute the module until it is really needed (require) to begin parsing,

Seajs is the correct way to load modules asynchronously, but the order in which the modules are executed is strictly in the order in which they appear in the code (require), so it is more logical. What do you say, Requirejs?

This is about executing (actually running the code in the Define) module, not the load (load file) module.

Module loading is parallel, no difference, the difference lies in the timing of the implementation of the module, or is resolved.

Module specification in JS (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.