Methods of introducing namespaces into JavaScript programming and code _javascript techniques

Source: Internet
Author: User
The most common syntax format for JAVASCRIPT code is to define functions function xxx () {/*code...*/}, often with such a large stack of function definitions. The function name is very easy to conflict, especially when the introduction of multiple JS files, the conflict is particularly obvious. Therefore, it is necessary to introduce namespaces.
Javascript itself has no concept of namespaces and needs to be modeled with objects.
For example, a class that defines a namespace for creating namespaces:

function NameSpace () {
}

This is a constructor, but does not do anything, and then the following comments related to the code:

var comment = new NameSpace ();
Comment.list = function () {/*code...*/};
Comment.counter = 0;

The first line creates the so-called namespace (which is actually a blank object), named comment, and line second to third defines two methods under that space. Call can be used comment.list () or comment.counter++, etc.;
To create a child namespace again:

Comment.add = new NameSpace ();
Comment.add.post = function () {/*code...*/}
Comment.add.check = function () {}

The concept of namespaces is introduced to avoid problems with the same function names. The above procedure can also be defined as this:

var comment = {
List:function () {/*code...*/},
Add: {
Post:function () {/*code...*/},
Check:function () {/*code...*/}
}
}

In Prototype.js, it's a lot of use in this way, although this way is more intuitive like a tree, but as long as the nodes slightly more, the eyes are busy looking for the relationship between these nodes, the approach of the namespace is to describe the relationship tree horizontally, the hierarchical relationship is directly manifested in the literal, two ways the effect is consistent, But the writing style has its own characteristics.

To extend a method again:

NameSpace.prototype.appendChild = function (ns) {
for (var key in NS) {
This[key] = Ns[key];
}
return this;
}
NameSpace.prototype.copyChild = NameSpace.prototype.appendChild;


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.