Javascript modular programming (1): how to write modules
As websites gradually become ” Internet applications “, the Javascript codes embedded in web pages become larger and more complex.
Web pages are more and more like desktop programs, requiring a team of division of labor, schedule management, unit testing, etc… Developers have to use software engineering methods to manage the business logic of the web.
Javascript modular programming has become an urgent need. Ideally, developers only need to implement the core business logic, and everything else can load modules already written by others.
However, Javascript is not a modular programming language. It does not support ” class “, let alone “module”. (The sixth edition of the ECMAScript standard , which is under development , will officially support “classes” and “modules”, but it will take a long time to be put into practical use.)
The Javascript community has made a lot of efforts to achieve the effect of “modules” in the existing operating environment. This article summarizes the current best practices of “Javascript Modular Programming” and explains how to put it into practice. Although this is not an elementary tutorial, you can understand it with a little understanding of the basic grammar of Javascript.
1. Original writing
A module is a set of methods to achieve a specific function.
Simply put together different functions (and variables that record state) together, and it’s a module.
function m1(){
//…
}function m2(){
//…
}
The above functions m1() and m2() form a module. When using it, just call it directly.
The shortcomings of this approach are obvious: “pollution” of global variables, there is no guarantee that variable name conflicts will not occur with other modules, and there is no direct relationship between module members.
2. Object writing
In order to solve the above shortcomings, the module can be written as an object, and all module members are placed in this object.
var module1 = new Object({
_count: 0,
m1: function (){
//…
},m2: function (){
//…
}});
The above functions m1() and m2() are all encapsulated in the module1 object. When it is used, the properties of this object are called.
module1.m1();
However, this way of writing will expose all module members, and the internal state can be rewritten externally. For example, external code can directly change the value of the internal counter.
module1._count = 5;
Three, execute function writing immediately
Use ” function is executed immediately ” (Immediately-Invoked Function Expression, IIFE), can not achieve the purpose of exposure of private members.
var module1 = (function(){
var _count = 0;
var m1 = function(){
//…
};var m2 = function(){
//…
};return {
m1: m1,
m2: m2
};})();
Using the above writing method, external code cannot read the internal _count variable.
console.info(module1._count); //undefined
module1 is the basic writing method of Javascript module. Next, we will process this writing.
Four, zoom mode
If a module is very large and must be divided into several parts, or one module needs to inherit another module, then it is necessary to adopt the “augmentation mode” (augmentation).
var module1 = (function (mod){
mod.m3 = function () {
//…
};return mod;
})(module1);
The above code adds a new method m3() to the module1 module, and then returns the new module1 module.
5. Loose augmentation
In the browser environment, each part of the module is usually obtained from the Internet, and sometimes it is impossible to know which part will be loaded first. If the writing method in the previous section is adopted, the first part of execution may load an empty object that does not exist. In this case, the “wide enlargement mode” should be used.
var module1 = (function (mod){
//…
return mod;
})(window.module1 || {});
Compared with the “enlargement mode”, the “wide enlargement mode” means that the parameter of the “execute function immediately” can be an empty object.
Six, input global variables
Independence is an important feature of a module, and it is best not to directly interact with other parts of the program within the module.
In order to call global variables within the module, other variables must be explicitly entered into the module.
var module1 = (function ($, YAHOO) {
//…
})(jQuery, YAHOO);
The module1 module above needs to use the jQuery library and the YUI library, so these two libraries (in fact, two modules) are input into module1 as parameters. In addition to ensuring the independence of the modules, this also makes the dependencies between the modules obvious. For more discussion on this, see Ben Cherry’s famous article “JavaScript Module Pattern: In-Depth” .
The second part of this series will discuss how to organize different modules and manage dependencies between modules in the browser environment.
(over)