Asynchronous modules define AMD and asynchronous modules amd

Source: Internet
Author: User

Asynchronous modules define AMD and asynchronous modules amd

It is called Asynchronous Module Definition, and is defined by Asynchronous components (or modules. AMD is a mechanism in which components and their dependencies can be asynchronously loaded.

Define Method

define(id?, dependencies?, factory);

Component ID

The component ID is the unique identifier of a component. In a script file that is one-to-one with the component ID (only one define in a script file), this ID can be absent, and it is best not. This is because the component loader must provide a component ID when requesting the component, and the script file path corresponding to the component ID needs to be configured. The loader can find the corresponding component script file based on the file path. Therefore, the loader can directly use the ID used in the request as the ID of this component. The advantage is that you do not need to modify the component ID when you transfer components to other projects because of ID conflicts. Therefore, you are advised not to set the component ID when defining components using the define method.

In addition, according to AMD specifications, the first parameter of the define method, the component ID must be a top-level or absolute ID, not a relative ID. This is more likely to cause component ID conflicts, so I think it is not necessary to set the component ID in the define method.

AMD believes that using define can help static analysis tools, such as build, is unnecessary because the component IDs are not found in define most of the time, and the analysis is meaningless.

According to AMD specifications, multiple components can be defined in a file, but each define must be assigned a component ID.

Component ID format

Note,The component ID displayed in the define method must be a top-level or absolute ID.. However, the component ID that appears in the require method or define dependency array can be Relative ID (Relative ID relative to the current component, rather than the path relative to the component), and is completelyCompatible with CommonJS specifications on Component ID formats.

CommonJS conventions on Component IDs:

L The component ID is composed of words separated by commas (,). Each word must be in the camper format, or it can be '.' or '..' to indicate the relative relationship;

L The component ID can have a suffix of '. js' or a suffix of'. js;

Example:

Assume that the component 'jquery/dialog 'corresponds to the path 'jquery/ui/dialog. js, and it depends on 'jquery/jquery. js', you need to go to 'Dialog. in the js file, write require as follows: require ('.. /jquery ') or require ('.. /jquery. js ').

Dependent Array

That is, the second array. The dependent array contains the dependent component ID. The component ID here allows relative IDs. If no dependency array is defined, ['require ', 'export', 'module'] is used by default. However, this depends on the number of parameters in the factory method. If there is only one parameter, then the dependent array can only be ['require '], and the other two can be pushed accordingly.

Factory

If factory is a method, its return value is the value developed by the component. If the factory is an object, the factory is an open value.

Define. amd

This attribute is an object, which indicates that the global define method complies with AMD specifications. What are amd attributes? AMD has no rules. You can write them as needed.

Require Method

AMD has lower requirements for the require method than define, and component loaders can be used at will, as long as they can work properly. For example, the require JS require method can require multiple components at a time, and the component loader according to CommonJS specifications can only require one component at a time.

CommonJS

According to CommonJS guidance, CommonJS does not have the define method, and the dependency is obtained completely through require ('module-id. Fortunately, in the browser, the Function object has a toString () method, which can be converted to text format. Search for require ('module-id') in the text to obtain all dependencies. The disadvantage is that the ability to load dependencies based on conditions is lost, but a brain is loaded in full.

AMD uses the define method to declare the module. CommonJS does not have the define method, so AMD and CommonJS are two different. However, according to the requirements of the second define parameter, we can see that AMD components can also have three variables: require, exports, and module. Therefore, copy the code of CommonJS component to the define method. Most codes can be used without modification. This is more convenient.

Note: The Function object of the Opera Mobile Phone platform does not have the toString () method. Therefore, scripts in the CommonJS format cannot run normally on this platform.

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.