JS's modular programming

Source: Internet
Author: User

Today, I learned a bit of JS modular programming, I feel that JavaScript is really profound, commonjs,requirejs,nodejs,seajs, this record a bit of experience. JavaScript design is not a modular programming language, the concept of ' class ' and ' module ', but ES6 will formally support "Class" and "module". With the module, we can more easily use other people's code, want what function, load what module. However, the premise is that you have to write the module in the same way, considering that the JavaScript module does not yet have an official specification, so various specifications emerge.

Let's take a look at what is modularity?

modularity is a method of separating the system into independent functional parts, which can strictly define the module interface and the transparency between modules. Modular thinking is more common in the background language.

What are the advantages and disadvantages of modularity?


(1) maintainability can be flexible architecture, to achieve focus separation, convenient single module function debugging, upgrade, convenient module combination, decomposition, multi-person collaboration and non-interference.

(2) can be divided into unit testing.


(1) The DOM operation support is weak, of course, can also use JQ;
(2) The operation of excessive instruction and event binding in HTML, high coupling degree

Two concepts are described below:

cohesion refers to the internal implementation of the module, which is a natural extension of the concept of information hiding and localization, it marks the internal components of a module of the integration of the degree of tightness.

The benefits are also obvious, and it's much easier to read the relevant tasks after grouping them.

the design should increase the cohesion of the module as much as possible, thus obtaining a high degree of module independence.

Coupling degree is a measure of the degree of association between modules. Coupling depends on the complexity of the interface between the modules, the location of the module to enter or call, and so on.

In contrast to cohesion, a loosely coupled system should be pursued as much as possible at design time.

Currently, there are two types of standard JavaScript modules:commonjs and Amd+cmd.

CommonJSIn 2009, American programmer Ryan Dahl created the node. JS project to use the JavaScript language for server-side programming. This symbol "JavaScript modular programming" was formally born. Because there is no module in the browser environment, there is no significant impact, but on the server side, must have the module, and the operating system and other applications to interact with, otherwise it is impossible to program. Node. JS's module system is implemented with reference to the COMMONJS specification. Now compare the fire of the React and the surrounding class library, is directly using the CommonJS module system, using NPM Management module, using browserify packaging output module. In Commonjs, there is a global method require () that is used to load the module. For example: To load a "math" module and use the method inside: var math = require (' math ');Math.add (2,3);//5COMMONJS specification has a great limitation, because he is synchronous loading, affected by the speed of the network is very large, if long time can not get the corresponding resources from the server, the page will be blocked, not load normally. Due to the limitations of the COMMONJS specification, it does not apply to the browser environment MyApp, so this time the advent of AMD and CMD, CMD is the domestic Yuber great God in the development of SEAJS when the proposed, belongs to the commonjs of a specification, in addition to AMD, is a requirejs in the promotion of a specification, also use require () to load the module. Both are an implementation of the asynchronous module definition (asynchronuous module definition) and do not affect the execution of subsequent statements;the difference between CMD and AMD:  CMD is equivalent to loading on demand, when defining a module does not need to make a dependent module immediately, when needed require can be, more convenient; AMD, on the other hand, requires a dependent module to be defined and introduced into the factory in the form of formal parameters. The CMD is highly dependent on the nearest, AMD is highly dependent on the predecessor.

Recommended Reading Nanyi Teacher's article feel very detailed.

JS's modular programming

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.