JAVASCRIPT

Namespaces

Article by:
Date Published:
Last Modified:

Overview

Unlike many object-orientated languages such as C++ and C#, namespaces are not natively supported in JavaScript.

However, they can be “faked” in a number of different ways. One of the easiest is to enclose everything within a variable:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var fakeNamespace = {

    NamespaceFunc1: function() {
    },

    NamespaceFunc2: function() {
    }
};

// You then call functions within it using the following syntax
fakeNamespace.NamespaceFunc1();

Notice though, you have to remember to include commas after every function or variable declaration. AND, you can’t declare private objects. But it is the simplest solution.


Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Tags

    comments powered by Disqus