One thing that JavaScript programmers have to deal with is corruption of the global namespace. Every time you define a simple function, or other variable at the top level of a web page, the names you've chosen could potentially come in conflict with names used by other developers or libraries that you are using. In the browser, all global variables become properties of the window object.
I've been dealing with this in an ad-hoc manner until recently. I would create a single global variable for all my code, and then define all my functions and variables within it, like this:
var PF = {
global: value,
...,
MyFunc: function(args)
{
...
},
...
};
I tend to want to migrate code from one project to another quite frequently, so putting all the code in one namespace was becoming quite tedious as I was editing the code to move it into different namespaces for different projects. Inspired by Python, I've developed a more general method of defining and importing namespaces across different modules/namespaces of javascript code.
Here is a typical way to define a new namespace, and import another namespace into it so you can reference code from other libraries succinctly.
global_namespace.Define('startpad.base', function(ns) {
var Other = ns.Import('startpad.other');
ns.Extend(ns, {
var1: value1,
var2: value2,
MyFunc: function(args)
{
....Other.AFunction(args)...
}
});
ns.ClassName = function(args)
{
};
ns.ClassName.prototype = {
constructor: ns.ClassName,
var1: value1,
Method1: function(args)
{
}
};
});
The benefits of this approach are:
There still remains a problem of javascript composition. I don't like to include lots of different script files in the same web page. So you still have to combine the source code from multiple different independent script files into one file. This can be done as part of a build process (along with javascript minification), or through a composition service running on your web server (I hope to write one of these in Python for my AppEngine projects).
I am placing namespace.js into the public domain. Let me know if you end up using it, or have suggestions for improvements.