AMD: module specifications in browsers

Source: Internet
Author: User

Modules/Wrappings makes implementation a reality. Although it is not exactly the same as the node. js module writing method, there are also many similarities, so that programmers familiar with node. js have some intimacy.

But NodeJS is the JavaScript on the server end after all, and there is no need to put these lines in the browser's JavaScript environment. At this time, AMD was born, and its full definition is called asynchronous module. It is known from the name that it is suitable for script tag. It can also be said that AMD is designed specifically for the JavaScript environment in the browser. It draws on some advantages of CommonJS, but does not copy its format. At first, AMD existed as the transport format of CommonJS because it was unable to reach an agreement with CommonJS developers and existed independently. It has independent wiki and discussion groups.

AMD has designed a simple write module API:Define.

Define (id ?, Dependencies ?, Factory );

Where:

◆ Id: module id, which can be omitted.

◆ Dependencies: the dependent module, which can be omitted.

◆ Factory: module implementation, or a JavaScript Object.

The id follows the CommonJS Module Identifiers. The order of the dependencies element corresponds to the factory parameter one by one.

The following is a simple three-tier structure (basic library, UI Layer, and application layer) developed in AMD mode ):

Base. js

 
 
  1. define(function() { 
  2.     return { 
  3.         mix: function(source, target) { 
  4.         } 
  5.     }; 
  6. }); 

Ui. js

 
 
  1. define(['base'], function(base) { 
  2.     return { 
  3.         show: function() { 
  4.             // todo with module base 
  5.         } 
  6.     } 
  7. }); 

Page. js

 
 
  1. define(['data', 'ui'], function(data, ui) { 
  2.     // init here 
  3. }); 

Data. js

 
 
  1. define({ 
  2.     users: [], 
  3.     members: [] 
  4. }); 

The preceding three usage methods of define are also demonstrated,

1. Define a non-dependent module (base. js)

2. Define the dependent modules (ui. js, page. js)

3. Define the data Object Module (data. js)

When you are careful, you will find that the module is not displayed, that is, the module named

 
 
  1. define('index', ['data','base'], function(data, base) { 
  2.     // todo 
  3. }); 

The named module is not recommended most of the time. It is generally used when multiple modules are merged by the packaging tool into a js file.

As mentioned above, the order of dependencies elements is one-to-one correspondence with factory, which is not very rigorous. AMD began to propose its own module style to get rid of CommonJS constraints. But later I made another compromise and was compatible with CommonJS Modules/Wrappings. That is, you can write it like this.

 
 
  1. define(function(require, exports, module) { 
  2.     var base = require('base'); 
  3.     exports.show = function() { 
  4.         // todo with module base 
  5.     }  
  6. }); 

Aside from a function layer, the format is similar to that of NodeJS. Use require to obtain the dependency module and use the exports export API.

In addition to define, AMD reserves a keyword require. As a globally reserved identifier, require can be implemented as module loader. Or not.

Currently, AMD libraries include RequireJS, curl, Dojo, bdLoad, JSLocalnet, and Nodules.

There are also many libraries that support AMD specifications and will be available as a module, such as MooTools, jQuery, qwery, bonzo, and even firebug.

Original article: http://www.cnblogs.com/snandy/archive/2012/03/12/2390782.html

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.