Creating the multiple namespace JS library with classes


When I started the creating new namespace JS library I got very usful artile to read.

http://pietschsoft.com/post/2007/07/Creating-Namespaces-in-JavaScript-is-actually-rather-simple.aspx

In this article they covered lot of things ( namespace protection objective )

Here I am using the namespace manager code:

// Create the Namespace Manager that we’ll use to
/// make creating namespaces a little easier.

if (typeof Namespace == ‘undefined’) var Namespace = {};
if (!Namespace.Manager) Namespace.Manager = {};

Namespace.Manager = {
Register:function(namespace){
namespace = namespace.split(‘.’);

if(!window[namespace[0]]) window[namespace[0]] = {};

var strFullNamespace = namespace[0];
for(var i = 1; i < namespace.length; i++)
{
strFullNamespace += “.” + namespace[i];
eval(“if(!window.” + strFullNamespace + “)window.” + strFullNamespace + “={};”);
}
}

};

// Register our Namespace
Namespace.Manager.Register(“Dummy.Utility.Class”);

Here I regestered the namspace.

Dummy.Utility.Class.dunnyClass = function() {

this.test= function(){alert(“test”);}

this.test2= function(){alert(“test2”);}

}

Using this namaspace JS class in HTML.

var dumdum = new Dummy.Utility.Class.dunnyClass();

dumdum.test();

dumdum.test2();

Advertisements
This entry was posted in javascript and tagged , , by Purab Kharat. Bookmark the permalink.

About Purab Kharat

I am WordPress and Drupal Architect. I worked on many technologies like Ruby on Rails, Flex, PHP etc... I am Purab Kharat from India, Pune. I love to play chess and listen music and read books. I work in SY Smartsourcing Global Pvt Ltd in Princeton, NJ, USA. Now I am living at Plainsboro, 164 Hampshire Drive, Princeton Meadows, NJ. I have more then Ten years experience in Web Application Development (Specially in Php 5.0, Ruby on Rails, Mysql 5.0, RIA, Wordpress, Drupal, Joomla, Ecommerce)

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