JavaScript modularization Specification

Source: Internet
Author: User
The JavaScript modularization specification mainly follows the CommonJS and AMD specifications. CommonJS specifications-server-side JavaScript specifications Node. js is a server-side JavaScript project that uses the CommonJS standard to implement its module system. In CommonJS, a global require method is used to load modules... the JavaScript modular specification mainly follows the CommonJS and AMD specifications. CommonJS specifications-server-side JavaScript specifications Node. js is a server-side JavaScript project that uses the CommonJS standard to implement its module system. In CommonJS, a global require method is used to load the module, which is implemented and completed by the native module. This module has been loaded at startup. There are two main types of modules: Native module and file module. 1. The native module is also the core module in Node. js. Native modules are compiled into binary execution files during Node. js source code compilation, with the fastest loading speed. Node. js contains many native modules, such as http, net, OS, path, fs, and module. The reference method is var fs = require ('fs'); 2. the file module of the file module is dynamically loaded, and the loading speed is slower than that of the native module. However, Node. js caches both the native module and the file module. During the second request for this module, there will be no repeated overhead. File modules can be subdivided into three categories. These three file modules are differentiated by file suffixes. Node. js determines the Loading Method Based on the suffix.. Js. Use the fs module to synchronously read js files and compile and execute them.. Node. Third-party modules installed by npm.. Json. Read the file and call JSON. parse to parse and load the file. Although Node. js uses the require method to introduce modules, the internal loading is not simple, and the priority is not the same. AMD specifications-browser-side JavaScript specifications: the module in CommonJS is stored locally and loaded in synchronous mode. The loading time is basically equivalent to the disk I/O time. This will not be a problem, but if the environment is a browser, the file loading is subject to network factors. If the network loading is slow, the browser may be suspended. Therefore, the browser can only load modules asynchronously. AMD: Asynchronous Module Definition (Asynchronous Module Definition ). The module is loaded asynchronously. The module loading does not affect the execution of the subsequent statements. All statements that depend on this module are defined in a callback function. After loading is complete, the callback function will run. AMD also uses the require () statement to load modules, but unlike CommonJS, it requires two parameters: require ([module], callback); the first parameter [module] is an array, the Member is the module to be loaded, and the second parameter callback is the callback function after successful loading. Currently, there are two major Javascript libraries that implement AMD specifications: require. js and curl. js. Require. js uses AMD specifications to implement module loading, which is defined by the define function. The define function accepts two parameters. The first parameter is the dependent array, and the second parameter is the module function to be defined. If no dependent module exists, the first parameter can be omitted.

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.