Embracing Modular Javascript_javascript Skills

Source: Internet
Author: User

Once again we are enveloped by the terminology of the computer.
Backbone, Emberjs, Spinejs, Batmanjs and other MVC frameworks have come to the attack.
Commonjs, AMD, Nodejs, Requirejs, Seajs, Curljs and other modular JavaScript are coming.
The concept of modular JavaScript is particularly strong and seems to be catching up with the 07 Ajax craze.
I. Write function (procedure type)
Until 2005, JavaScript was not taken seriously, only as a form of verification, such as a small number of applications. At that time a Web page can not write a few lines of JS code, 1000 line is very complicated. When you organize your code in a way that is a process, dozens of of lines of code don't even have to write a function. A little more need to extract abstraction out of a function, more complex some need more functions, call each other between functions.


Second, write Class (object-oriented)
2006, Ajax swept the world. JavaScript is valued, and more and more backend logic is put on the front end. The number of JS code in the Web page increases dramatically. In this case, write function way to organize a large number of code appears powerless. Sometimes debugging a small function, from a function may jump to the nth function. At this time, the way of writing class appeared, Prototype first popular. It organizes the code, writes out each class, each class is created by Class.create. There are Yui, ext and other heavyweight framework. Although they are written in different ways, but they are designed to meet a large number of JavaScript code development.

Third, write module (now, future?) )
2009, Nodejs was born! This server-side JavaScript quickly conquered the browser-side jser by using modular notation. Cattle have emulated, all kinds of writing module norms are endless. Commonjs wants to unify the front and back of the writing, AMD thinks it is suitable for browser-side. Well, whatever the style of the writing module, the JavaScript that writes modularity has become popular. You ready? (Uh, inflammatory)

Oh, what is modular JavaScript? Is this how we invented another silver bullet? Whatever it is, study it. As to the suitability of the use of the project, their respective discretion.

It says nothing about "modules". In fact, in the computer field, the concept of modularity has been praised for nearly 40 years. The overall structure of the software embodies the modular idea that the software is divided into separate named parts, each part is called a module, when all the modules are assembled together, you can get a solution to the problem.

Modularity is based on a divide-and-conquer approach, but does it mean that we subdivide the software indefinitely. In fact, when the segmentation is too meticulous, the total number of modules increases, the cost of each module is reduced, but the cost of module interfaces increases. To ensure the rational segmentation of the module, it is necessary to understand information hiding, cohesion and coupling degree.

Information Hiding
The module should be designed so that the information (procedures and data) it contains is not visible to modules that do not need it. Each module completes a separate function and then provides an interface for that functionality. Modules are accessed through interfaces. JavaScript can be hidden by functions, encapsulated, and then returned to the interface object. The following is a module event that provides event management.

Copy Code code as follows:

event = function () {
Todo
return {
Bind:function () {},
Unbind:function () {},
Trigger:function () {}
};
}();

In order to implement the desired interface bind, unbind, trigger may need to write a lot of code, but the code (process and data) for other modules do not have to disclose, external as long as the interface can be accessed Bind,unbind,trigger.

Information hiding is very obvious to module design, it not only supports the parallel development of modules, but also reduces the workload of testing or later maintenance. If you want to modify the code later, the hidden part of the module can be changed arbitrarily, provided the interface is unchanged. When the event module began to implement in order to compatible with the old version of IE and Standard browser, wrote a lot of IE special Code, one day the old version ie disappeared (se years), simply delete it.

Cohesion Degree
Cohesion refers to the internal implementation of the module, which is the natural extension of the concept of information hiding and localization, which indicates the close degree of the integration of each component within a module. The benefits are also obvious, and it is much easier to read the relevant tasks when grouped.
Design should be as much as possible to improve the cohesion of the module, so as to obtain higher module independence.

Coupling degree
Cohesion is a measure of the internal implementation of a particular module, and the degree of coupling is the measure of the correlation between modules. The degree of coupling depends on the complexity of the interface between modules, the position of the module entering or calling. In contrast to cohesion, a loosely coupled system should be pursued as far as possible in design.

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.