JavaScript Modular programming-relationships between CommonJS, AMD and RequireJS

Source: Internet
Author: User

Let's talk about CommonJS first-do you think JavaScript is just a client compilation language? In fact, JavaScript is not just a language designed for the client at the beginning. Later, due to the rapid popularity of Web, and the conflict between Netscape and Microsoft, it was too early to standardize ipvcip. For details about the JS history, see http://zh.wikipedia.org/zh-cn/javascript. Early standardization of JS leads to many defects of JS and the lack of standard class libraries. Even this will not affect JS to become an excellent programming language (such as Node. js, which is very popular now ). Currently, JavaScript only includes basic APIs. CommonJS is an organizational unit if it is not used as a server-side programming language, such as IO, FS, i18n, and package, it allows JS to work in the same direction to improve JS. As far as possible, no official version of Common JS has been released yet, but the implementation of many drafts has achieved good results. For example, Node. js is now very popular. Back to the topic of this article, what is the relationship between CommonJS and AMD and RequireJS? In the past, CommonJS had a Modules specification. It was very difficult for us to write everything like JS in a file to write the server-side application, it is used to solve JS's lack of modular code management function. The key part is two functions: require-used to introduce dependency export-used to export modules, including identifier and contents CommonJS is not just a specification, just like interfaces in Java, it does not indicate how to implement them. The problem is that this Modules specification of CommonJS was designed for the server end at the beginning, and it is a synchronous mode. However, this mode is not suitable for browsers. We imagine that if the browser synchronization mode loads modules one by one, opening the module will become very slow, so AMD is born for this, its biggest feature is that it can load modules asynchronously. The difference is that AMD has a define function that allows the current module to load the modules that the current module depends on when running, for example, the following definition means to load the dependent module, dependency, and array module before running the function. Define ('module/id/string', ['module', 'dependency ', 'array'], function (module, dependency, array) {return ModuleContents ;}); so CommonJS Module and AMD are both JS modular APIs. from the same origin, JavaScript can be loaded in a modular manner. In fact, RequrieJS is the most widely used and popular Implementation of AMD. The introduction on the RequireJS website also illustrates the reason why RequireJS was born, but I didn't understand it at the time. RequireJS is a JavaScript file and module loader. it is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. using a modular script loader like RequireJS will improve the speed and quality of your code. from the above, we can see the relationship between CommonJS (commonly referred to as the Modules Specification) and AMD and RequireJS. To put it simply, CommonJS Modules and AMD both aim to solve the JS modular standard API. CommonJS is more suitable for Server, while AMD is basically used for browser (however, it can also be used for Server, for example, Node loader direction of efforts: http://requirejs.org/docs/node.html), and RequireJS is AMD's most popular implementation.

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.