Modular programming of JavaScript (ii) __ algorithm

Source: Internet
Author: User

At present, there are two kinds of standard JavaScript modules: Commonjs and AMD. AMD is mainly introduced here, but will start from Commonjs. One, Commonjs 2009, American programmer Ryan Dahl created the Node.js project, which uses JavaScript language for server-side programming. This sign "JavaScript modular Programming" is officially born. Because frankly speaking, in the browser environment, no module is not particularly big problem, after all, the complexity of the Web page program is limited, but on the server side, must have a module, and the operating system and other applications to interact, or simply can not program. The modular system of Node.js is realized by reference to COMMONJS specification. In Commonjs, there is a global method require (), which is used to load modules. Assuming there is a mathematical module math.js, you can load it like this:

	var math = require (' math ');
You can then invoke the method provided by the module:
	var math = require (' math ');
	Math.add (2,3);//5
Because this is mainly for browser programming, does not involve node.js, so the COMMONJS is not introduced. The main idea here is that require () is used to load modules. second, the browser environmentWith the server-side module, it's natural for everyone to want the client module. And it's best to be compatible, a module that doesn't have to be modified and can be run on both the server and the browser. However, due to a significant limitation, the COMMONJS specification does not apply to the browser environment. If the above code runs in a browser, there is a big problem: The second line of Math.add (2,3) runs after the first row require, so math.js load must be completed, that is, if the load time is long, the entire application will stop there. This is not a problem for the server side, because all the modules are stored on the local hard drive, can be completed synchronously, waiting time is the hard disk read time. However, for browsers, this is a big problem, because the module is placed on the server side, waiting time depends on the speed of the network, may have to wait for a long time, the browser in the state of "suspended animation." Therefore, the browser-side module, can not take "synchronous load", only "asynchronous load." This is the background of the birth of AMD specification. Third, AMDAMD is the abbreviation for "Asynchronous module definition", meaning "asynchronous modular definition". It loads the module asynchronously, and the load of the module does not affect the operation of the statement behind it. All statements that rely on this module are defined in a callback function, and the callback function will not run until the load is complete. AMD also uses the Require () statement to load modules, but unlike COMMONJS, it requires two parameters:
	Require ([module], callback);
The first parameter [module], which is an array whose members are the modules to be loaded, and the second parameter callback, is the callback function after the success of the load. If you rewrite the previous code into AMD, this is the following:
	Require ([' math '], function (math) {
		Math.add (2, 3);
	});
Math.add () is not synchronized with the math module, and the browser does not feign death. So it's clear that AMD is better suited to the browser environment. Currently, there are two JavaScript libraries that implement AMD specifications: Require.js and Curl.js.
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.