JavaScript simple implementation of namespace effect _javascript skills

Source: Internet
Author: User

JavaScript native does not support namespaces and needs to be implemented in a workaround.

Namespaces are important when we create a JavaScript library, and we can encapsulate the fragmented JavaScript file (*.js) that makes up this JavaScript library in a namespace without having to define a global function or class. For example, a person who appears more than once in this section, we can encapsulate it in the appropriate namespace as part of the library:

Code 5-13:

Copy Code code as follows:

var com = {};
Com.anyjava = {};
Com.anyjava.Person = function (name) {
Private members
var _name = name;

Access device
This.getname = function () {
return _name;
};
This.setname = function (name) {
_name = name;
};
};
Prototype
Com.anyjava.Person.prototype = {
Eat:function () {
Alert (This.getname () + "is eating something.");
},
Sleep:function () {
Alert (This.getname () + "is sleeping.");
},
Walk:function () {
Alert (This.getname () + "is walking.");
}
};
var dirk = new Com.anyjava.Person ("Dirk");
Dirk.eat ();

From code 5-13, we get a namespace that matches the Java developer's habits and specify our command space path when instantiating the person object.

Here's a tip, if you're using a JavaScript library that someone else has developed and with a more complete namespace plan, you might be tired of writing a lengthy namespace every time. For example, you are using the JavaScript library I developed, under the Com.anyjava.control.ui namespace, there are a lot of extended UI controls you want to use, and I don't think you want to write many times var xxx = new Com.anyjava.control.ui.XXX (). By specifying the namespace alias, we can write less repetitive code, as shown in code 5-14, another way to instantiate person in code 5-13:

Code 5-14:

Copy Code code as follows:

var ns = Com.anyjava;
var dirk = new NS. Person ("Dirk");
Dirk.eat ();

The last thing I'm going to say is that you need to be aware of a problem when using namespaces. When writing a JavaScript library, most of the time namespace declaration statements may appear in multiple locations in a JavaScript file, or multiple JavaScript files appear. But the JavaScript language attribute is that the last declared variable overrides a variable of the same name declared earlier, which requires us to pay attention to the issue of repeated declarations, which means that each time a Namespace object is declared, it is advisable to determine whether the namespace object already exists, as shown in code 5-15:

Code 5-15:

Copy Code code as follows:

if (typeof Com.anyjava = = "undefined") var Com.anyjava = {};

So we can make sure that the "Com.anyjava" object is declared only once.

Related Article

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.