Namespace.js

Following my previous article on object oriented programming in Javascript, I released yesterday a small script which allows you to manage namespaces. I know that solutions already exist. Not considering the one from libraries (I wanted a library independent script), I was not satisfied by any of them.

Namespace.js is very simple to use. It contains all common actions encountered when dealing with namespaces. It can import objects in the global scope or include remote scripts based on their namespace. It is very small (5.4 Kb minified), open source and of course library independent.

I published the project on Github at https://github.com/maximebf/Namespace.js

Here are some examples:

Namespace('foo.bar');
foo.bar.myFunction = function() {};

Namespace('com.example', {
   MyClass: function() { return {}; }
});
var obj = new com.example.MyClass();

Namespace.use('com.example.MyClass');
var obj2 = new MyClass();

// include com/example/RemoteClass.js
Namespace.include('com.example.RemoteClass');
var obj3 = new com.example.RemoteClass();

Namespace.registerNativeExtensions();
'com.foo.bar'.namespace();


comments powered by Disqus

26/02/2009