Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to create a JavaScript namespace? [closed]

Tags:

javascript

I have not yet found a common way on the Internet for creating a namespace in JavaScript.

What's the best way to create a namespace (and please list any downfalls that particular approach might have).

like image 930
Timmy Avatar asked Jul 18 '10 04:07

Timmy


People also ask

How is Namespacing done in JavaScript?

JavaScript does not provide namespace by default. However, we can replicate this functionality by making a global object which can contain all functions and variables.

What is JavaScript namespace pollution?

Polluting Global namespace causes name collision. This name collision is very common in large projects where we may be using several javascript libraries. Let's discuss in detail what a name collision is. let's take a scenario in which 2 teams named A1 and A2 are working on a project.

What is namespace in node JS?

Namespaces are a TypeScript-specific way to organize code. Namespaces are simply named JavaScript objects in the global namespace. This makes namespaces a very simple construct to use. Unlike modules, they can span multiple files, and can be concatenated using outFile .


3 Answers

(function() {

    var usefulVariable;

    var foo = {
      bar:function(){ alert('hi') }
    };

    // leak into global namespace
    window.foo = foo;

})();

Only foo is exposed to the global namespace, and it "lives" within your private self executing anon function namespace.

like image 64
meder omuraliev Avatar answered Oct 06 '22 23:10

meder omuraliev


In JavaScript, namespaces can only be achieved through object literals, which can be as simple as this:

var MyNS = {
    f1: function() {...},
    f2: function() {...}
};

As others have attempted to show, if you want to provide a private scope within your namespace (which is suggested), the following method is the most appropriate:

var MyNS = (function() {
    var private1 = "...", 
        private2 = "...";

    return {
        f1: function() {...},
        f2: function() {...}
    }
})();
like image 39
Justin Johnson Avatar answered Oct 07 '22 00:10

Justin Johnson


The book Pro JavaScript Design Patterns (Harmes & Diaz, 2008) provides examples of namespace creation using simple object literals or self-executing anonymous functions, both of which are illustrated in Justin Johnson's excellent answer, and these approaches work well.

Depending on the situation, it might be possible that the namespace already exists and contains members. In such a case, these approaches would destroy any members in the namespace. If this is a concern, one can use an approach similar to this to preserve any existing members, while adding new ones:

var myNamespace = (function(ns) {
    ns.f1 = function() { ... };
    ns.f2 = function() { ... };

    return ns;
})(window.myNamespace || {});

Here, myNamespace is assigned the value returned by the anonymous, self-executing function that receives the parameter ns. As one can see, the value of this parameter is either window.myNamespace or an empty object, depending on whether or not myNamespace has been previously declared.

like image 39
Rick Avatar answered Oct 06 '22 23:10

Rick