Module and namespace in Javascript

Source: Internet
Author: User

Modules and namespaces in JavaScript are not differentiated.
Each defined function is an attribute of a global object. The most important rule that JavaScript code module must follow is to avoid defining global variables. Because, when defining a global variable, it is dangerous to be covered by other modules, so the modular encoding should use the following method:
VaR moduleclass = {};
Moduleclass. function name 1 = function (){
Function body; // This function looks like an object method. Yes. You can use an object as a namespace.
}
Moduleclass. function name 2 = function (){
Function body;
}
Use an object as a namespace and place all functions and variables in it. In this way, even if the function or variable has the same name (that is, different objects have the same function name), they are not in the same namespace, so there is no risk of being overwritten.
The first rule of JavaScript modularization: a module should not add more than one tag to the global namespace. Explanation: var moduleclass ={} in the above example is actually an attribute in the global namespace (Global object. In layman's terms: Apart from defining a module namespace for a global namespace, do not write any other code.
Put the above code into a *. js file so that the module can be reused, and the file name must be consistent with the namespace name. Suppose we put the above Code into the moduleclass. js file (in this case, your namespace must be consistent with the file name ). Now there is another new problem:
What if a name conflict occurs? That is, two people use the same file name at the same time. As you know, files in the same directory cannot have the same file name, so you can put these two files under different directories. For example, util/moduleclass. js and tools/moduleclass. js. In this case, the space in our file cannot be the same as before, but as shown below:
Util/moduleclass. js
VaR util;
If (! Util) util = {}; // Level 1 Domain Name
Util. moduleclass ={}; // second-level domain name
Util. moduleclass. function name 1 = funciton (){
Function body;
}
Util. moduleclass. function name 2 = function (){
Function body;
}
Tools/moduleclass. js code:
VaR tools;
If (! Tools) Tools ={}; // Level 1 Domain Name
Tools. moduleclass ={}; // second-level domain name
Tools. moduleclass. function name 1 = function (){
Function body; // This function looks like an object method. Yes. You can use an object as a namespace.
}
Tools. moduleclass. function name 2 = function (){
Function body;
}
In this way, there will be no conflict. Of course, some people may ask, if some people define the same folder name, then it will not conflict? First of all, this is unlikely. If so, we can adopt the Java Naming method. As follows:
Namespace: COM. Company Name. Project name. util. Space name;
Actual path: COM/company name/project name/util/space name. js
The following code uses the namespace com. util. moduleclass as an example:
VaR com;
If (! Com) com ={}; // If com does not exist, a new one is generated.
Else if (typeof com! = "Object") {// if the object already exists but is not an object, an exception is thrown.
Throw new error ("xxxxxxxxxxxxxxxxxxxxxxx ");
}
If (! Com. util) COM. util = {}; // If com. util does not exist, a new one is generated.
Else if (typeof com. util = "object") {// If com exists but is not an object, an exception is thrown.
Throw new error ("xxxxxxxxxxxxxxxxxxxxxxxx ");
}
If (! Com. util. moduleclass) {// If com. util. moduleclass exists, an exception is thrown directly.
Throw new error ("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx ");
}
Com. util. moduleclass = {// If com. util. moduleclass does not exist, the code defined in this namespace can be used normally.
Function 1: function () {function body ;}
Function 2: function () {function body ;}
};

From: http://han2000lei.javaeye.com/blog/342810

However, I typed the article manually and thought that Ctrl + C and CTRL + V didn't make much sense ..

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.