Creating the multiple namespace JS library with classes

When I started the creating new namespace JS library I got very useful 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:

01// Create the Namespace Manager that we'll use to
02/// make creating namespaces a little easier.
03 
04if (typeof Namespace == 'undefined') var Namespace = {};
05if (!Namespace.Manager) Namespace.Manager = {};
06 
07Namespace.Manager = {
08Register:function(namespace){
09namespace = namespace.split('.');
10 
11if(!window[namespace[0]]) window[namespace[0]] = {};
12 
13var strFullNamespace = namespace[0];
14for(var i = 1; i < namespace.length; i++)
15{
16strFullNamespace += "." + namespace[i];
17eval("if(!window." + strFullNamespace + ")window." + strFullNamespace + "={};");
18}
19}
20 
21};
22 
23// Register our Namespace
24Namespace.Manager.Register("Dummy.Utility.Class");
25 
26Here I regestered the namspace.
27 
28Dummy.Utility.Class.dunnyClass = function() {
29 
30this.test= function(){alert("test");}
31 
32this.test2= function(){alert("test2");}
33 
34}

Using this namaspace JS class in HTML.

1var dumdum = new Dummy.Utility.Class.dunnyClass();
2 
3dumdum.test();
4 
5dumdum.test2();

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.