Namespacing in Javascript

Namespacing is a way to organize your classes and objects. This doesnt come built-in in javascript, so in order to get it we gotta cheat. Everything in javascript is an object (whether it be a variable, a function or just an object itself) and each object can have attributes. This is where we take advantage of the situation.

We can achieve namespacing by doing this:

var house = {};
house.pets = {}
house.pets.Dog = function(){};
house.pets.Cat = function(){};

explanation: i created an object named ‘house’. Then, i added a property to it called pets, which is also an object. Then i added two functions to it,Dog and Cat. I can later on instantiate them by doing:


var kitten = new house.pets.Cat();
var pup = new house.pets.Dog();

An easier way to do this is to use this piece of code i found online and slightly modified:


function namespace(ns_string)
{
 var parts = ns_string.split(".");
 var parent = window;

for(var i=0;i<parts.length;i++)
 {
 currentPart = parts[i];
 parent[currentPart] = parent[currentPart] || {};
 parent = parent[currentPart];
 }

return parent;
}

Now, you can easily create namespaces like this:


var pets = namespace("house.pets");
pets.Dog = function(){};
pets.Cat = function(){};

The function is used to get the namespace, and creates it if it doesnt exist. It will be especially useful when the depth or your namespaces exceed 2 sub-namespaces or so.

Another useful function is this:

function using_namespace(ns_string)
{
 var ns = namespace(ns_string);
 for(i in ns)
 window[i]=ns[i];
}

It works just like ‘using namespace’ in c++. Use it when ur namespaces are too long.

using_namespace("house.pets");
var hound = new Dog();

Why use namespaces?

As i said earlier, its for organizational purposes. Also, its to prevent classes or object from defined in other external scripts from overriding yours. Remember that your script wont be the only one included in whichever website that the code is being used in. When the namespace seems too long and you’re sure its contents wont change some other objects that were defined then use the using_namespace function. You can use encapsulation to fix this problem, it will be in another post.

here’s the code:

function namespace(ns_string)
{
 var parts = ns_string.split(".");
 var parent = window;
for(var i=0;i<parts.length;i++)
 {
 currentPart = parts[i];
 parent[currentPart] = parent[currentPart] || {};
 parent = parent[currentPart];
 }
return parent;
}
function using_namespace(ns_string)
{
 var ns = namespace(ns_string);
 for(i in ns)
 window[i]=ns[i];
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s