A detailed introduction to the concept of commonjs& Requirejs&seajs

Source: Internet
Author: User

With the development of the current side, the traditional development can not meet the requirements of front-end performance and engineering, so as a senior front-end developer, we must learn the front-end modularization knowledge;

The following main talk about, Common.js require.js&sea.js these three related introduction;

Common.js

CommonJS is associated with node. JS, node application is composed of modules, the adoption is CommonJS module specification;

The CommonJS API fills this gap by defining APIs that are used by many common applications, primarily non-browser applications. Its ultimate goal is to provide a library of standards like Python,ruby and Java. In this way, developers can write applications using the COMMONJS API, which can then be run in different JavaScript interpreters and different host environments. In a system compatible with COMMONJS, you can develop a practical JavaScript program:

Server-side JavaScript applications
Command-line tools
Graphical interface Applications
Hybrid applications (e.g., titanium or Adobe AIR)

Commonjs defined module is divided into: {Module reference (require)} {module definition (exports)} {module identification (modules)}

Require () is used to introduce external modules; The exports object is used to export the method or variable of the current module, the only export, and the module object represents the modules themselves.

AMD and Requirejs AMDAsynchronous module definition, is the asynchronous module definitions, all modules will be loaded asynchronously, the module load does not affect the subsequent statement run.     All statements that depend on certain modules are placed in the callback function. The AMD specification defines a function that is either a free variable or a global variable define.

define (ID?, dependencies, factory);    

AMD Spec Https://github.com/amdjs/amdjs-api/wiki/AMD The first parameter ID is a string type, which represents the module ID, which is an optional parameter. If it does not exist, the module identity should be defined by default as the identity of the requested script in the loader.    If present, then the module ID must be a top-level or an absolute identity.    The second parameter, dependencies, is an array literal that the current module relies on to identify the module that has been defined by the module.     The third argument, factory, is a function or an object that needs to be instantiated. Create modules identified as alpha, dependent on require, export, and modules identified as Beta
function (Require, exports, beta) {    function() {        return  beta.verb ();         // or:        return require ("beta"). verb ();    }});
RequirejsOfficial website Http://www.requirejs.org/API http://www.requirejs.org/docs/api.html Requirejs is a front-end modular management tool library that complies with AMD's specifications and is the author of AMD The standard founder James Burke.     So it's not too requirejs to say that the AMD specification is being explained. The basic idea of requirejs is that a function is used to load all required or dependent module implementations, and then return a new function (module), all of our business code about the new module is inside the function, and the inside of it can use the modules that have been loaded since. CMD and Seajs CMDIn cmd, a module is a file in the form of: define (factory);The global function, define, is used to define the module.    The parameter factory can be a function, or it can be an object or a string. When factory is an object, a string, the interface that represents the module is the object, the string. SeajsOfficial website Http://seajs.org/docs/API Quick Reference https://github.com/seajs/seajs/issues/266
Sea.js Core Features: 1.        Follow the CMD specification, with Nodejs-like writing module code. 2. Rely on automatic loading, the configuration is clear and concise. What is the difference between AMD and CMD? Here is Yuber's explanation for the difference between AMD and CMD: AMD is the 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.      Similar to the CommonJS modules/2.0 specification, is bravojs in the promotion process of the module definition of standardized output still a lot??    These specifications are intended for the modular development of JavaScript, especially on the browser side.      At present, the implementation of these specifications can achieve the purpose of browser-side modular development. Difference: 1. For dependent modules, AMD is executed ahead of time, and CMD is deferred execution. 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. The code is as follows:
//CMDDefinefunction(Require, exports, module) {varA = require ('./a ')) a.dosomething ()//100 lines are omitted here    varb = require ('./b ')//Reliance can be written nearb.dosomething ()// ...})//the default recommended by AMD isdefine (['./a ', './b '),function(A, B) {//dependence must be written right from the starta.dosomething ()//100 lines are omitted hereb.dosomething ()// ...})

A detailed introduction to the concept of commonjs& Requirejs&seajs

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.