Asynchronous module definition AMD

Source: Internet
Author: User

All called asynchronous module definition, an asynchronous component (or module) definition. AMD is a mechanism by which a component and its dependencies can be asynchronously loaded.

Define method

Define (ID?, dependencies, Factory);

Component ID

The component ID is a unique identifier for the component, and the ID is not available in the script file (only one define in a script file) with the component ID. This is because the component loader must give a component ID when requesting the component, and it needs to configure the script file path corresponding to the component ID. The loader can find the corresponding component script file based on the file path. Therefore, the loader can directly use the ID of the request as the ID of this component. The benefit is that when components are moved to other projects, the component ID needs to be modified without fear of ID collisions, so it is best not to set the component ID when using the Define method to define the component.

Also, according to the AMD specification, the first parameter of the Define method, the component ID must be a top-level or absolute ID and cannot be a relative ID. This is more likely to cause component ID collisions So, I think it is more inappropriate to set the component ID in the Define method.

AMD thinks that using define can help with static analysis tools, such as build, and I don't think it's necessary because there are more times when the component ID is not in define, and parsing doesn't make sense.

According to the AMD specification, it is also possible to define multiple components in a file, but this must assign a component ID to each define.

Component ID Format

Note that the component ID that appears in the Define method must be either a top-level or an absolute ID. However, the component ID that appears in the Require method or in the dependent array of define can be a relative ID (relative to the current component's ID, not the path to the component), and is fully compatible with the COMMONJS specification of the Component ID format .

COMMONJS conventions about component IDs:

L component IDs consist of words separated by '/', each of which must be a camel-shaped notation, or '. ', '. ', which is used to denote relative relationships;

L The component ID can have '. js ' suffix, or there can be no '. js ' suffix;

Example:

If the component ' Jquery/dialog ' corresponds to the path ' jquery/ui/dialog.js ', and it relies on ' jquery/jquery.js ', it is necessary to write require in the ' dialog.js ' file: Require ('. /jquery ') or require ('. /jquery.js ').

Dependent arrays

The second array, the dependent array contains the dependent component IDs, where the component IDs allow relative IDs. If you do not define a dependent array, the theoretical default is [' Require ', ' exports ', ' module '], but this can depend on the number of arguments factory this method. If there is only one parameter, then the dependent array can be just [' require '], the other two, and so on.

Factory

If factory is a method, its return value is the value that the component developed externally. If factory is an object, then factory is the value of opening to the outside.

Define.amd

This property is an object, and having this attribute indicates that the global define method complies with the AMD specification. As for AMD's properties, AMD does not stipulate that it can write on its own.

Require method

AMD's requirements for the Require method are lower than the define, and the component loader is free to play as long as it is guaranteed to work properly. For example, the Require method of Requirejs can require multiple components at a time, and a component loader based on the COMMONJS specification may only require one component at a time.

CommonJS

According to Commonjs's guidance, COMMONJS is not define method, and relies entirely on require (' Module-id ') acquisition. Fortunately, in a browser, the function object has a ToString () method that converts the method to a text format. All dependencies can be obtained by searching for require (' Module-id ') in the text. The disadvantage is that it loses the ability to load dependencies based on conditions, but a brain load.

AMD and COMMONJS are two different things, with the Define method declaring the module, COMMONJS there is no define method. However, according to the requirements of the second parameter of define, it can be seen that AMD components can also have require, exports, module three variables, so the COMMONJS component of the code copied to the Define method, most of the code can be used without modification, This is still more convenient.

Note: The function object of the opera mobile platform does not have the ToString () method, so scripts in the COMMONJS format will not function properly on this platform.

Asynchronous module definition AMD

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.