views:

44

answers:

2

Does the JavaScript frameworks which mimics namespaces like jQuery makes some namespace collision detection ? If not should I manage it myself ?

If yes what prevents such framework to detect such collision themselves ?

+3  A: 

JavaScript namespaces are normally mimicked by using objects and closures, and often initialized with a self-invoking function:

var myNamespace = (function () {
   var _name = 'Bob';

   return {
      somePublicMethod: function () {
         return 'Hello, ' + _name;
      }
   };
})();

alert(myNamespace.somePublicMethod());

Unfortunately if you redefine the namespace variable, there's no warning for that. What you could really do is to check if the namespace variable has already been defined, and throw an exception or raise an error if it was:

if (typeof myNamespace !== 'undefined') {
    var myNamespace = (function () {
        // ...
    })();
}
else {
    throw new Error("Whoops! myNamespace already exists.");
}
Daniel Vassallo
`!========` ? :)
galambalazs
@galambalazs: lol :) fixed :)
Daniel Vassallo
+1  A: 

Consider coming up with a development standard where entire team agrees on how you will call your namespaces. Also I found it useful reviewing any changes to data structure or namespaces before actually implementing them.

vikp
But this is not possible if your framework is distributed worldwide like jquery.