| Subcribe via RSS

Tutorial : Namespaces for javascript

January 27th, 2009 | No Comments | Posted in Javascript

Have you ever had a problem using somebody method with the same name. Well, Have you ever thought why javascript don’t have package or namespaces like other languages. Well, change your mind. This is how you will be implementing namespaces for javascript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<script type="text/javascript">
if ( com == null || typeof( com ) != "object" )
{
    var com = new Object();
}

if ( com.noppanit == null || typeof( com.noppanit ) != "object" )
{
    com.noppanit = new Object();
}

com.noppanit =
{
    message : function()
    {
        alert('test');
    },
    message1 : function()
    {
        alert('test1');
    }
;

com.noppanit.message();
com.noppanit.message1();

</script>